Insert users information inside database java on android studio - android-studio

Good evening to all,
I am creating an application with java on android studio.but after creating my database and making the registration page, the information does not go into the database.the errors I had said that there is a colons not created, I checked and everything is in place. when I remove the code that allows the insertion of information in the database the application works if not it is blocked at the stage of registration.I also tried nothing
A part of my database code
public static final String DATABASE_NAME = "Login.db";
public static final String TABLE_SIGN = "userss";
//public static final String TABLE_PROSPECT = "prospect";
public static final int DATABASE_VERSION = 1;
public static final String ID_COL= "id";
public static final String NAME_COL = "name";
public static final String USERNAME_COL = "username";
public static final String EMAIL_COL = "email";
public static final String PHONE_COL = "phone";
public static final String PASSWORD_COL = "password";
public MyDbHandler(Context context) {
super(context,DATABASE_NAME,null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ TABLE_SIGN
+ "(" + ID_COL + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"+
NAME_COL + "TEXT,"+ USERNAME_COL + "TEXT," + EMAIL_COL + "TEXT," + PHONE_COL
+ " TEXT,"+ PASSWORD_COL + "TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SIGN);
}
public boolean insertData(String name, String username, String email, String phone, String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME_COL, name);
contentValues.put(USERNAME_COL,username);
contentValues.put(EMAIL_COL,email);
contentValues.put(PHONE_COL,phone);
contentValues.put(PASSWORD_COL,password);
long result = db.insert(TABLE_SIGN, null,contentValues);
if(result == -1)
{
return false;
}else
{
return true;
}
}
The registration methods
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nam = name.getText().toString();
String user = username.getText().toString();
String email = mail.getText().toString();
String phoneN = phoneNo.getText().toString();
String pass = password.getText().toString();
if(nam.equals("") && user.equals("") && email.equals("") && phoneN.equals("") && pass.equals(""))
{
Toast.makeText(SignUp.this,"Veuillez remplir tout les champs",Toast.LENGTH_SHORT).show();
}
else{
Boolean regResult = db.insertData(nam,user,email,phoneN,pass);
if(regResult == true)
{
Toast.makeText(SignUp.this,"Connexion réussie avec succès",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),LogIn.class);
startActivity(intent);
}
else
{
Toast.makeText(SignUp.this,"Echec de connexion",Toast.LENGTH_SHORT).show();
}
}
}
});
The errors that i have:
2021-06-23 22:51:16.489 28861-28861/com.example.suiviprospection E/SQLiteLog: (1) table userss has no column named email
2021-06-23 22:51:16.490 28861-28861/com.example.suiviprospection E/SQLiteDatabase: Error inserting phone=60005882 email=shiv#gmail.com name=GOGNON Shiva password=Shiv username=shiv
android.database.sqlite.SQLiteException: table userss has no column named email (code 1): , while compiling: INSERT INTO userss(phone,email,name,password,username) VALUES (?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.suiviprospection.MyDbHandler.insertData(MyDbHandler.java:76)
at com.example.suiviprospection.SignUp$1.onClick(SignUp.java:48)
at android.view.View.performClick(View.java:5637)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Thank you for helping me , I'm waiting for your answers

Related

dependent spinner with sqlite isn't working

`I'm trying to create a dependent spinner with data from sqlite on android studio. But the spinners isn't showing any content as intended. The Prefix is suppose to narrow down the selection for the Lotno and finally display the Plantation_name based on database SmartSawit.db. pls help :> (its been days)
Spinner spinnerPrefix, spinnerLotno, spinnerPlantation_name;
Context context;
database Database;
String prefixValue, lotnoValue, nameValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daftar_pokok);
spinnerPrefix = findViewById(R.id.spinnerPrefix);
spinnerLotno = findViewById(R.id.spinnerLotno);
spinnerPlantation_name = findViewById(R.id.spinnerPlantation_name);
context = this;
Database = new database(this, "SmartSawit.db", 1);
try {
Database.CheckDB();
fillSpinner(context,spinnerPrefix, "registered_plantation", "prefix", "");
spinnerPrefix.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
prefixValue = parent.getItemAtPosition(position).toString();
fillSpinner(context,spinnerLotno,"registered_plantation","lotno","where prefix = '"+prefixValue+"'");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerLotno.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
lotnoValue = parent.getItemAtPosition(position).toString();
fillSpinner(context,spinnerPlantation_name,"registered_plantation","plantation_name","where prefix = '"+prefixValue+"' and lotno ='"+lotnoValue+"'");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}catch(Exception e){
e.printStackTrace();
}
}
#SuppressLint("Range")
private void fillSpinner (Context context, Spinner mSpinner,String table, String column, String where) {
SQLiteDatabase db = Database.OpenDatabase("registered_plantation.db");
ArrayList<String> mArray = new ArrayList<>();
Cursor cursor = db.rawQuery("Select distinct "+column+" from "+table+""+where, null);
while (cursor.moveToNext()){
mArray.add(cursor.getString(cursor.getColumnIndex(column)));
}
cursor.close();
db.close();
ArrayAdapter mAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item,mArray);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(mAdapter);
}
}
my databasehelper code:
private Context context;
private static final String DATABASE_NAME = "SmartSawit.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "registered_plantation";
//private static final String COLUMN_NO = "_no";
private static final String COLUMN_NAME = "plantation_name";
private static final String COLUMN_PREFIX = "prefix";
private static final String COLUMN_LOT_NUMBER = "lotno";
String DbPath;
Context mcontext;
String DbName;
public database(Context context, String name, int version) {
super(context, DATABASE_NAME, null, version);
this.context = context;
this.mcontext = context;
this.DbName = DATABASE_NAME;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.DbPath = context.getFilesDir() + "/database/";
}
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_NAME + " TEXT PRIMARY KEY, " +
COLUMN_PREFIX + " TEXT, " +
COLUMN_LOT_NUMBER + " INTEGER);";
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void CheckDB(){
SQLiteDatabase checkDB = null;
String filePath = DbPath + DbName;
File file = new File(filePath);
if(file.isFile() && file.exists()){
Toast.makeText(mcontext, "already exist", Toast.LENGTH_SHORT).show();
} else {
CopyDatabase();
}
}
private void CopyDatabase(){
try {
InputStream ios = mcontext.getAssets().open(DbName);
File directory = new File(DbPath);
if(!directory.exists()){
directory.mkdirs();
}
OutputStream os = new FileOutputStream(DbPath + DbName);
byte[] buffer = new byte[1024];
int len;
while((len = ios.read(buffer)) >0) {
os.write(buffer,0, len);
}
os.flush();
ios.close();
os.close();
Log.d("CopyDb", "Databse Copied");
} catch (Exception e) {
e.printStackTrace();
}
}
void addLadang (String prefix, String namaLadang, int lotNo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PREFIX, prefix );
cv.put(COLUMN_NAME, namaLadang );
cv.put(COLUMN_LOT_NUMBER, lotNo );
long result = db.insert(TABLE_NAME,null, cv);
if (result == -1){
Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "SUCCESSFULLY SAVED", Toast.LENGTH_SHORT).show();
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
public SQLiteDatabase OpenDatabase(String dbName){
String filePath = DbPath + dbName;
return SQLiteDatabase.openDatabase(filePath,null,0);
}
}
You have a number of issues (not actually with the spinners).
First you are trying to open a non-existent database. That is you are using the the table name not the database name in the fillSpinner method by using:-
SQLiteDatabase db = Database.OpenDatabase("registered_plantation.db");
i.e. the database name is SmartSawit.db not registered_plantation.db.
The following fixes that issue (SEE EMBEDDED COMMENTS):-
SQLiteDatabase db = Database.OpenDatabase(/*"registered_plantation.db"<<<<<<<<<<< TABLE NAME NOt DATABASE NAME so USED >>>>>>>>>>*/ database.DATABASE_NAME /*after making it public */);
You then have issues with missing spaces in the WHERE clauses again in the fillSpinner method (fixed using, again see comments):-
Cursor cursor = db.rawQuery("Select distinct "+column+" from "+table+" "/*<<<<<<<<<< ADDED SPACE*/+where, null);
Applying the above fixes and using a layout that has the 3 spinners side by side and with different coloured backgrounds and with an excessive height. And with the following data in the database (in the assets folder name SmartSawit.db):-
Then when first run:-
Clicking on the first spinner
i.e. A and B, the 2 prefixes are selected accordingly
Clicking on B and
i.e. as expected
and so on.
Additional
If you checked the log with your original code, even though it doesn't crash you could have spotted the issue e.g. the log would have included:-
2023-02-07 11:13:59.641 24792-24792/a.a.so75365360javasqlitespinners D/CopyDb: Databse Copied
2023-02-07 11:13:59.642 24792-24792/a.a.so75365360javasqlitespinners E/SQLiteLog: (14) cannot open file at line 36667 of [c255889bd9]
2023-02-07 11:13:59.642 24792-24792/a.a.so75365360javasqlitespinners E/SQLiteLog: (14) os_unix.c:36667: (2) open(/data/user/0/a.a.so75365360javasqlitespinners/files/database/registered_plantation.db) -
2023-02-07 11:13:59.643 24792-24792/a.a.so75365360javasqlitespinners E/SQLiteDatabase: Failed to open database '/data/user/0/a.a.so75365360javasqlitespinners/files/database/registered_plantation.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:211)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:195)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:503)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:204)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:196)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:880)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:865)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:766)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
at a.a.so75365360javasqlitespinners.database.OpenDatabase(database.java:124)
at a.a.so75365360javasqlitespinners.MainActivity.fillSpinner(MainActivity.java:69)
at a.a.so75365360javasqlitespinners.MainActivity.onCreate(MainActivity.java:36)
So although the copy was successfully accomplished e.g. using Device Explorer you would something along the lines of:-
Thus it is easy to see that registered_plantation.db is not at the expected location but SmartSwait.db is.
In short. If using try/catch and things are not working as expected the Check the Log for what you expect to see in the log (e.g. looking for Database copied would have made it easy to see that things were not as expected as the error lines immediately follow that message).

SQL Server - Login failed for user (Android Studio)

I have written code using JDBC to create registration page in Android Studio. I create database in SQL Server 2019. In addition I tried to connect from the code to MSSQL database and insert values, but while I run the app I'm getting the error:
login failed for user.
I have examined many options that can cause this error and I fixed them. For example: The server authentication is SQL Server authentication,the TCP option at the server is enabled, I have the relevant permissions of the user, also I check the if the firewall is the cause for this error. However, I continue to receive the error.
The connection class:
public class ConnectionClass {
public static String ip= "***.***.*.*:1433"; //
public static String user_name ="****";
public static String password = "****";
public static String database = "tests";
}
The Registeration class:
EditText email;
EditText password;
EditText confirmPassword;
Button sign_up;
EditText name;
Connection connection1;
Statement statement;
TextView status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up_parent);
email = (EditText) findViewById(R.id.editTextTextEmailAddress2);
password = (EditText) findViewById(R.id.editTextTextPassword);
confirmPassword = (EditText) findViewById(R.id.editTextTextPassword2);
sign_up = (Button) findViewById(R.id.button);
name = (EditText) findViewById(R.id.editTextTextPassword3);
status = (TextView) findViewById(R.id.status);
sign_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new ParentSignUp.registerUser().execute("");
}
});
}
public class registerUser extends AsyncTask<String, String, String>{
String z ="";
Boolean isSuccess = false;
public registerUser() {
super();
}
#Override
protected void onPreExecute() {
status.setText("Sending Data to Database");
}
#Override
protected void onPostExecute(String s) {
name.setText("");
email.setText("");
password.setText("");
confirmPassword.setText("");
}
#Override
protected String doInBackground(String... strings) {
try {
connection1 = connection_class(ConnectionClass.user_name.toString(), ConnectionClass.password.toString(),
ConnectionClass.database.toString(), ConnectionClass.ip.toString());
if (connection1 == null) {
z = "Check Internet Connection";
}
else {
String sql = "INSERT INTO test (name,password, email) VALUES ('example', 'example', 'example')";
statement = connection1.createStatement();
statement.executeUpdate(sql);
}
}
catch (Exception e) {
isSuccess = false;
z = e.getMessage();
}
return null;
}
}
#SuppressLint("NewApi")
public Connection connection_class (String user, String password, String database, String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String connectionUrl = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connectionUrl = "jdbc:jtds:sqlserver://" + server + "/" + database + ";user=" + user + ";"+"password=" + password + ";";
connection = DriverManager.getConnection(connectionUrl);
}catch (Exception e){
Log.e("SQL Connection Error : ", e.getMessage());
}
return connection;
}

How to Search value showing wrong index to update and delete in SQLite?

Search Function
mSearchView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d("data", charSequence.toString());
}
#Override
public void onTextChanged(CharSequence charSequence2, int i, int i1, int i2) {
String select = "SELECT * FROM RECORD WHERE name LIKE '"+charSequence2+"%'";
Cursor cursor = mSQLiteHelper.getData(select);
mList.clear();
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
mList.add(new Model(id,name,phone));
}
mAdapter.notifyDataSetChanged();
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Update: This is LongClickListener where I tap to updated and delete my inserted data
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
//Alert dialog to display options of update and delete
final CharSequence [] items = {"Update","Delete","Call"};
AlertDialog.Builder dialog = new AlertDialog.Builder(RecordListActivity.this);
dialog.setTitle("Choose an Action");
dialog.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0){
//update
Cursor c = mSQLiteHelper.getData("SELECT id FROM RECORD");
ArrayList<Integer> arrID = new ArrayList<Integer>();
while(c.moveToNext() ){
arrID.add(c.getInt(0));
}
//Show update Dialog
showDialogUpdate(RecordListActivity.this,arrID.get(position));
}
if(i==1){
//delete
Cursor c = mSQLiteHelper.getData("SELECT id FROM RECORD");
ArrayList<Integer> arrID = new ArrayList<Integer>();
while(c.moveToNext()){
arrID.add(c.getInt(0));
}
showDialogDelete(arrID.get(position));
}
//Call try
if(i==2){
TextView tvPhone = view.findViewById(R.id.textphone);
String phone = tvPhone.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phone));
getBaseContext().startActivity(intent);
}
}
});
dialog.show();
return true;
}
});
Model: This is the Model
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
DataBase
public void updateData(String name, String phone, int id){
SQLiteDatabase database = this.getWritableDatabase();
//Query to update record
String sql = "UPDATE RECORD SET name=? , phone=? WHERE id=?";
SQLiteStatement statement = database.compileStatement(sql);
statement.bindString(1,name);
statement.bindString(2,phone);
statement.bindDouble(3,(double)id);
statement.execute();
database.close();
}
//Delete Data
public void deleteData(int id){
SQLiteDatabase database = this.getWritableDatabase();
//query to delete record using id
String sql = "DELETE FROM RECORD WHERE id=?";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindDouble(1,(double)id);
statement.execute();
database.close();
}
//Getting Data
public Cursor getData(String sql){
SQLiteDatabase database = this.getReadableDatabase();
return database.rawQuery(sql,null);
}
Dialog
//DeleteDialog
private void showDialogDelete(final int idRecord) {
AlertDialog.Builder dialogDelete =new AlertDialog.Builder(RecordListActivity.this);
dialogDelete.setTitle("Warning!!");
dialogDelete.setMessage("Are you sure you want to delete this?");
dialogDelete.setPositiveButton("OK", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try{
mSQLiteHelper.deleteData(idRecord);
Toast.makeText(getApplicationContext(), "Delete Successfully", Toast.LENGTH_SHORT).show();
}catch (Exception error){
Log.e("Delete error",error.getMessage());
}
}
});
dialogDelete.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialogDelete.show();
}
//UpdateDialog
private void showDialogUpdate(Activity activity, int position){
Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.update_dialog);
dialog.setTitle("Update");
final EditText updateNameId = dialog.findViewById(R.id.updateNameId);
final EditText updatePhoneId = dialog.findViewById(R.id.updatePhoneId);
final Button updatebuttonId = dialog.findViewById(R.id.updatebuttonId);
//get Data Row Clicked from SQLite
Cursor cursor = mSQLiteHelper.getData("SELECT * FROM RECORD WHERE id="+position);
mList.clear();
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
updateNameId.setText(name);
String phone = cursor.getString(2);
updatePhoneId.setText(phone);
mList.add(new Model(id,name,phone));
}
//set width of dialog
int width = (int)(activity.getResources().getDisplayMetrics().widthPixels * 0.95);
//set height of dialog
int height = (int)(activity.getResources().getDisplayMetrics().heightPixels * 0.7);
dialog.getWindow().setLayout(width,height);
dialog.show();
updatebuttonId.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
try{
mSQLiteHelper.updateData(
updateNameId.getText().toString().trim(),
updatePhoneId.getText().toString().trim(),
position);
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
}catch(Exception error){
Log.e("Update error",error.getMessage());
}
updateRecorderList();
}
});
}
private void updateRecorderList() {
//get all data from SQLite
Cursor cursor = mSQLiteHelper.getData("SELECT * FROM RECORD");
mList.clear();
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
mList.add(new Model(id,name,phone));
}
mAdapter.notifyDataSetChanged();
}
When I search any data in the search bar It shows me the searched data but when I click to update it, it shows me the first row data to update, and also when I click to delete it shows me the first row to delete, where is my wrong logic can you enlighten me? anyone
You took the ID incorrectly, because you used a position that was always changing and cannot be correlated with the data.
You do not have to go to the database for the ID, as you already have it:
if (i == 0) {
//update
int contactId = mList.get(position).getId();
showDialogUpdate(RecordListActivity.this, contactId);
}
if (i == 1) {
//delete
int contactId = mList.get(position).getId();
showDialogDelete(contactId);
}

SQlite Data is not updating into listview

SQlite Data is not updating into listview. no error showing just not save the edited value. when I click the list view it will open with the inserted value to edit but when I push the update button it won't update. Any Solution for my Problem ??
ListDataActivity.java
listView = findViewById(R.id.listViewId);
loadData();
}
public void loadData() {
//Create an arraylist so that i can load the data to add in the arraylist
ArrayList<String> listData = new ArrayList<>();
Cursor cursor = databaseHelper.showAllData();
//jodi kono cursor e row na thake
if (cursor.getCount() == 0) {
Toast.makeText(getApplicationContext(), "no data is available", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textViewId, listData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedValue = adapterView.getItemAtPosition(position).toString();
cursor.moveToPosition(position);
String name = cursor.getString(1);
String surName = cursor.getString(2);
// listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
Intent intent = new Intent(ListDataActivity.this, UpdateActivity.class);
Toast.makeText(getApplicationContext(), "Selected Value : " + selectedValue, Toast.LENGTH_LONG).show();
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("surName", surName);
startActivity(intent);
finish();
}
});
UpdateActivity.java
id = getIntent().getExtras().getLong("id");
String name = getIntent().getExtras().getString("name");
String surName = getIntent().getExtras().getString("surName");
updateName.setText(name);
updatePhone.setText(surName);
updateButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String name = updateName.getText().toString();
String surName = updatePhone.getText().toString();
if(view.getId()==R.id.updatebuttonId){
ListDataActivity.databaseHelper.updateInformation(id,name,surName);
startActivity(new Intent(UpdateActivity.this,ListDataActivity.class));
finish();
}
}
});
DatabaseHelper.Java
public int updateInformation(long id, String name, String surName) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(SURNAME,surName);
String whereArgs[] = {""+id};
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,NAME+ "=?",whereArgs);
return count;
}
I believe that your issue is that you are trying to update using a WHERE clause that is effectively saying update the row where the NAME = the id that has been passed (likely to never find such a row).
You need to change the 3rd parameter passed to the update method to be the name of the id column. So you want something like :-
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,ID+ "=?",whereArgs); //<<<<< ID instead of NAME
Note the above assumes that the String Constant ID contains the name of the id column. So you may have to change ID accordingly.

Why Cassandra throwing com.datastax.driver.core.exceptions.InvalidQueryException: Multiple definitions found for column

Context:
I am running a jUnit test in eclipse by using embedded Cassandra to test my DAO class which is using an Astyanax client configured for JavaDriver. When DAO object instance insert into Cassandra I am getting this exception com.datastax.driver.core.exceptions.InvalidQueryException: Multiple definitions found for column ..columnname
TestClass
public class LeaderBoardDaoTest {
private static LeaderBoardDao dao;
public static CassandraCQLUnit cassandraCQLUnit;
private String hostIp = "127.0.0.1";
private int port = 9142;
public Session session;
public Cluster cluster;
#BeforeClass
public static void startCassandra() throws IOException, TTransportException, ConfigurationException, InterruptedException {
System.setProperty("archaius.deployment.applicationId", "leaderboardapi");
System.setProperty("archaius.deployment.environment", "test");
EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
// cassandraCQLUnit = new CassandraCQLUnit(new
// ClassPathCQLDataSet("simple.cql", "lbapi"), "cassandra.yaml");
Injector injector = Guice.createInjector(new TestModule());
dao = injector.getInstance(LeaderBoardDao.class);
}
#Before
public void load() {
cluster = new Cluster.Builder().withClusterName("leaderboardcassandra").addContactPoints(hostIp).withPort(port).build();
session = cluster.connect();
CQLDataLoader dataLoader = new CQLDataLoader(session);
dataLoader.load(new ClassPathCQLDataSet("simple.cql", "lbapi"));
session = dataLoader.getSession();
}
#Test
public void test() {
ResultSet result = session.execute("select * from mytable WHERE id='myKey01'");
Assert.assertEquals(result.iterator().next().getString("value"), "myValue01");
}
#Test
public void testInsert() {
LeaderBoard lb = new LeaderBoard();
lb.setName("name-1");
lb.setDescription("description-1");
lb.setActivityType(ActivityType.FUEL);
lb.setImage("http:/");
lb.setLbId(UUID.fromString("3F2504E0-4F89-41D3-9A0C-0305E82C3301"));
lb.setStartTime(new Date());
lb.setEndTime(new Date());
dao.insert(lb);
ResultSet resultSet = session.execute("select * from leaderboards WHERE leaderboardid='3F2504E0-4F89-41D3-9A0C-0305E82C3301'");
}
#After
public void clearCassandra() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
#AfterClass
public static void stopCassandra() {
EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
}
}
Class under test
#Singleton
public class LeaderBoardDao {
private static final Logger log = LoggerFactory.getLogger(LeaderBoardDao.class);
#Inject
private AstyanaxMutationsJavaDriverClient client;
private static final String END_TIME = "end_time";
private static final String START_TIME = "start_time";
private static final String IMAGE = "image";
private static final String ACTIVITY_TYPE = "activity_type";
private static final String DESCRIPTION = "description";
private static final String NAME = "name";
private static final String LEADERBOARD_ID = "leaderboardID";
private static final String COLUMN_FAMILY_NAME = "leaderboards";
private ColumnFamily<UUID, String> cf;
public LeaderBoardDao() throws ConnectionException {
cf = ColumnFamily.newColumnFamily(COLUMN_FAMILY_NAME, UUIDSerializer.get(), StringSerializer.get());
}
/**
* Writes the Leaderboard to the database.
*
* #param lb
*/
public void insert(LeaderBoard lb) {
try {
MutationBatch m = client.getKeyspace().prepareMutationBatch();
cf.describe(client.getKeyspace());
m.withRow(cf, lb.getLbId()).putColumn(LEADERBOARD_ID, UUIDUtil.asByteArray(lb.getLbId()), null).putColumn(NAME, lb.getName(), null).putColumn(DESCRIPTION, lb.getDescription(), null)
.putColumn(ACTIVITY_TYPE, lb.getActivityType().name(), null).putColumn(IMAGE, lb.getImage()).putColumn(START_TIME, lb.getStartTime()).putColumn(END_TIME, lb.getEndTime());
m.execute();
} catch (ConnectionException e) {
Throwables.propagate(e);
}
}
/**
* Reads leaderboard from database
*
* #param id
* #return {#link LeaderBoard}
*/
public LeaderBoard read(UUID id) {
OperationResult<ColumnList<String>> result;
LeaderBoard lb = null;
try {
result = client.getKeyspace().prepareQuery(cf).getKey(id).execute();
ColumnList<String> cols = result.getResult();
if (!cols.isEmpty()) {
lb = new LeaderBoard();
lb.setLbId(cols.getUUIDValue(LEADERBOARD_ID, null));
lb.setName(cols.getStringValue(NAME, null));
lb.setActivityType(ActivityType.valueOf(cols.getStringValue(ACTIVITY_TYPE, null)));
lb.setDescription(cols.getStringValue(DESCRIPTION, null));
lb.setEndTime(cols.getDateValue(END_TIME, null));
lb.setStartTime(cols.getDateValue(START_TIME, null));
lb.setImage(cols.getStringValue(IMAGE, null));
} else {
log.warn("read: is empty: no record found for " + id);
}
return lb;
} catch (ConnectionException e) {
log.error("failed to read from C*", e);
throw new RuntimeException("failed to read from C*", e);
}
}
}
When the Java driver throws an InvalidQueryException, it's rethrowing an error from Cassandra. The error "Multiple definitions found for column..." indicates that a column is mentioned more than once in an update statement. You can simulate it in cqlsh:
cqlsh> create table test(i int primary key);
cqlsh> insert into test (i, i) values (1, 2);
code=2200 [Invalid query] message="Multiple definitions found for column i"
I'm not familiar with Astyanax, but my guess is that it already adds the id to the query when you call withRow, so you don't need to add it again with putColumn. Try removing that call (second line in reformatted sample below):
m.withRow(cf, lb.getLbId())
.putColumn(LEADERBOARD_ID, UUIDUtil.asByteArray(lb.getLbId()), null)
... // other putColumn calls

Resources