I am trying to update a single column in a SQLite database table in my Android project. What I want to do is set the value of "done" column to 1, when the vehicle number is same. I preferred to go with sql queries other than using myDB.update. So I used this query,
update 'details' set done = 1 where vehicle=="*****"
But the problem is, this query is working perfectly in the sqlite databse browser, not in android simulator. This is completely not working at the android simulator. Hope ur advice.
Thanks in advance.
//Declaration
SQLiteDatabase dbx;
ContentValues cv1;
EventDataSQLHelper eventsData;//object of class in which table is created
//on create
eventsData = new EventDataSQLHelper(this);
dbx= eventsData.getReadableDatabase();
cv1 = new ContentValues();
cv1.put("done",1);
//update query
dbx.update("details", cv1, "vehicle=" ? , null);
Related
I'm trying to update a table in SQLite android. I have a column called 'quantity' which stores qty of some items, say item1, item2 ...
Now when I purchase item1, I'd definitely want to 'add' the purchased qty to an existing qty of item1.
I searched the web but couldn't find a solution, hence asking this.
My simple code's below:
// This method is used to 'UPDATE' the table 'stock'.
// This method will be used by two fragments,
// 'sale' and 'purchase' fragments.
public int updateData(String cigaretteName,int quantity, int cost, int totalCost) {
// Accessing the database with writable functionality so it can be updated.
SQLiteDatabase db = this.getWritableDatabase();
// Creating content values object to put the new values in existing rows with old values.
ContentValues contentValues = new ContentValues();
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
contentValues.put(StockEntry.COLUMN_COST, cost);
contentValues.put(StockEntry.COLUMN_TOTAL_COST, totalCost);
// Which row to update, based on the cigarette name.
String selection = StockEntry.COLUMN_CIGARETTES_NAME + " LIKE ?";
String[] selectionArgs = {cigaretteName};
// Updating the table with the new values and then returning the number of rows affected.
return db.update(StockEntry.TABLE_NAME, contentValues, selection, selectionArgs);
}
This isn't working at all, now it doesn't even update the column/row.
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
Do help guys!
I would suggest simple approach to overcome these kind of SQLite related issues.
Use SQLite Manager which is plugin for FireFox browser
Download from https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Create your dummy database there
Perform your CRUD operations here
Once everything working fine in SQLite Manager then use same query inside your project.
Above way will save your development time as well as testing.
I am working in an android studio project and I have a login activity,I created database using SharedPreferences and SQLite;I have created a table which has columns id,email,password,check_in,check_out.
I made it possible to login after I register a user;but I do not know how to insert a data in check_in cell of logged user.
So my problem is that how to make possible to add the time of check_in in table in database after logged in?
And how I can the see the table created using SQLite?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("check_in", value);
contentValues.put("check_out", value);
// this will insert if record is new, update otherwise
db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
I made it possible to login after I register a user;but I do not know
how to insert a data in check_in cell of logged user.
You would use the SQLiteDatabase's update method something along the lines of :-
ContentValues cv = new ContentValues();
cv.put("check_in",System.currentTimeMillis());
SQLiteDatabase db = this.getWritableDatabase();
db.update("your_table_name",cv,"email=? AND user=?",new String[]{email,user});
And how I can the see the table created using SQLite?
You can
install a product that provides this ability,
copy the database via adb or tools that utilise adb, so that a product that can look at SQLite databases can access the copied file,
extract a cursor from the table and then use this as the source for displaying the data e.g you could use something like
:-
SQLiteDatabae db = this.getWriteableDatabase();
Cursor csr = db.query("your_table_name",null,null,null,null,null,null);
Databaseutils.dumpCursor(csr);
csr.close();
I referred the following pages but still my problem is not solved,
Android sqlite update table always return 0 , SQLite in Android How to update a specific row , Android SQLite update row not working , Update Function in android SQLite is not working and SQLiteDatabase update not working?
I have a sqlite database as QUESTIONS(ID,QUES,ANS),ID is the primary key. When i try to update a value using following code no change happens in the database.
SQLiteDatabase db = null;
ContentValues values = new ContentValues();
values.put("QUES","New title");
values.put("ANS","Answer" );
long k= db.update("QUESTIONS",values,"ID=2",null);
Log.d("Success",""+k);
The value of k is 1, And there is no change in database. I also tried with
long k= db.update("QUESTIONS",values,null,null);
But no use .
Your first line sets db to be null.
You have no database configured for the UPDATE command to work on.
My problem solved
db = openOrCreateDatabase("data", Context.MODE_PRIVATE, null);
I’m using a database helper to update a table with one row and two fields, I have the following code that that sends two phone numbers through.
dbHelper.updateNumbers(newSmsNumber, newVoiceNumber);
and the following method in the helper.
public void updateNumbers(String newSmsNumber, String newVoiceNumber) {
//Update code here
}
Can anyone show me the code I need to add in the method to update the two fields in the database.
Cheers,
Mike.
ContentValues cv = new ContentValues();
cv.put("SMS", newSmsNumber);
cv.put("Voice", newVoiceNumber);
db.update("[table name]", cv, "ID=?", new String[]{Integer.toString(id)});
There are some gaps to fill up though, the table name, and how you identify the entry you want to update (I put a "ID" field there in that example)
Did not run that code, did not really check, but that should give you an idea.
I am working on a project where I need to store multiple sessions. When a user logs in the system, his username is stored in the app. So the next time app is run, the username is displayed on the home screen. He can either click on that or add new account, which then would get populated in the list too. I read the notepad tutorial on android developers website to learn how to use SQLite in android, but I couldn't make it work for my case.
I am storing the username in a string variable 'value' and I want this 'value' to be added in the database and displayed as a list on the screen.
Can anyone please help me on this? IF you could give some links of tutorials that do exactly what I am trying to do, that would help too. Thanks!
declare this first
private SQLiteDatabase db;
to create table
this.db=this.openOrCreateDatabase("filestore", MODE_PRIVATE, null);
this.db.execSQL("CREATE TABLE IF NOT EXISTS usernames(name text)");
insert
String username;
db.execSQL("insert into usernames values ('"+username+"')");
retrieve
this.db = this.openOrCreateDatabase("filestore", MODE_PRIVATE, null);
db_results = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT name FROM usernames", null);
while (cursor.moveToNext()) {
db_results.add(String.valueOf(cursor.getString(cursor
.getColumnIndex("name"))));
// db_results.add(String.valueOf(cursor.getString(cursor.getColumnIndex("name"))));
}
cursor.close();
db.close();
This tutorial from markana helped me to grab the basics of the sqlite and android. Hope it'll help you too.
another example, and one more too