In my activity I have a spinner with data taken from SQLite db by this query:
private List<String> ottieniAnni(){
List<String> result = new LinkedList<String>();
SQLiteDatabase db = new BHelper(this).getReadableDatabase();
String sql = "SELECT DISTINCT strftime('%Y',"+GTable.DATE+") FROM "+GTable.TABLE_NAME;
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()){
result.add(c.getString(0));
}
db.close();
return result;
}
Now through button, I want to delete all records in the year chosen by the user. I used the method db.delete (), but so will delete all the records.
Should I set a parameter but do not know how to do it:
SQLiteDatabase db= mHelper.getWritableDatabase();
db.delete(GiornateTable.TABLE_NAME, null, null );
db.close();
Try like
SQLiteDatabase db= mHelper.getWritableDatabase();
int rowcount = db.delete(GiornateTable.TABLE_NAME,
"GIVE YOUR COLUMN NAME" + "=?",
new String[] { "GIVE YOUR VALUE" });
Log.d("No of record deleted",String.valueOf(rowcount));
you can use the same syntax as you do with the select, only for deleting
Related
I have retrieved 2 values through the putExtra and getStringExtra method in another class. BOth these values are correct. I need to use these 2 values in my SQL query:
final Cursor cursor = sdb.rawQuery("SELECT match_player_name FROM match_response WHERE match_date=? AND " +
"match_response =?"+new String [] {String.valueOf(date),String.valueOf(response)}, null);
I am unfamiliar using rawQuery with more than 1 condition. Have I the correct syntax? I have a record in my SQLite database satisfying this condition.
This is an example:
Cursor cur = database.rawQuery("select name from Table where ID=? and SubCategoryID=?",
new String [] {String1,String2});
public Cursor getMachineServices(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select row1, from maquina where something = x AND somethingelse = y", new String[]{});
return c;
}
Then on your activity where you want to display it you must do:
Database db;
db = new Database(yourActivity);
Cursor c = db.getMachineServices);
if (c.moveToFirst()) {
do {
String machines
c.getString(c.getColumnIndex("row1"));
//you can use the variable machines where you want
} while (c.moveToNext());
}
I want to check the records are available or not in my Sqlite table. I tried but the cursor is always return 1 value even if the SQlite databse table is empty, no any record in table. Why return one? How to solve this issue?
dbhelper = new MyDbHelper(this);
SQLiteDatabase db = dbhelper.getWritableDatabase();
String count = "select count(*) from ActivityObjectList";
Cursor mcursor = db.rawQuery(count, null);
mcursor.moveToFirst();
int icount = mcursor.getInt(0);
if(icount>0)
{
Log.e("Record does not found","");
//leav
}
else
{
Log.e("Record is exists !!","");
//populate table
}
Use mcursor.getCount() to check number of rows.
I'm having some trouble saving a string with an accent to my database and retrieving it.
This is my function that saves a new location to the database. It gets 'myId' and 'location' and inserts them. The println there shows the item as I expect, with the accent. The example I'm using is Mazzarrà.
public long createLocationRecord(String location, int myId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_OWMID, myId);
values.put(KEY_NAME, location);
System.out.println("newItem in DB = "+location);
long callSQL = db.insertWithOnConflict(TABLE_LOCATION, null, values, SQLiteDatabase.CONFLICT_IGNORE);
if(callSQL==-1)
db.update(TABLE_LOCATION, values, KEY_OWMID + '=' + owmId, null);
return callSQL;
}
This is my function to retrieve all location items. The println here prints out Mezzarra, without the accented à. Am I missing something? Do I need to make a change to my database? It's just a regular Android SQLite DB that I'm opening via SQLiteBrowser.
public ArrayList<String> getLocationList() {
ArrayList<String> list = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * "
+ "FROM " + TABLE_LOCATION
+ " ORDER BY _id ASC";
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
if (c.moveToFirst()) {
do {
System.out.println("newItem GETTING FROM DB = "+c.getString(c.getColumnIndex(KEY_NAME)));
list.add(c.getString(c.getColumnIndex(KEY_NAME)));
} while (c.moveToNext());
}
c.close();
return list;
}
Thanks for any help anyone can provide.
I created and populated a SQLite database with the Firefox SQLite Manager. I copied the .sqlite file into the assets folder of my Android project.
My aim is to use this records to populate a spinner. When I query the database, the cursor displays the columns but no records.
My DatabaseHandler class extends SQLiteOpenHelper and contains methods for onCreate, onUpgrade and getAllOffices.
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT OfficeId as _id, OfficeName FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
offices.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
Any ideas on why the records are not returned would be appreciated.
Try this:
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT OfficeId as _id, OfficeName FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
offices.add(cursor.getString(cursor.getColumnIndex("_id")));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
Basically it will just ensure that you are using the correct column index. Otherwise if you are using eclipse then try the cellobject plugin to browse the database and make sure your table is populated as expected:
http://marketplace.eclipse.org/content/cellobject-sqlite-xml-browser#.UWOU5RaBD3g
Please try this and may be useful for you.
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
System.out.println("CURSOR SIZE:"+cursor.getCount())
// looping through all rows and adding to list
if(cursor.getCount()!=0){
if (cursor.moveToFirst()) {
do {
System.out.println(cursor.getString(c.getColumnIndex("as per your db column name"));
} while (cursor.moveToNext());
}
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
First of all you have to just see console output if you are getting right then add into your office bean
I have an Android app that has a database. My database has a table say Person Table, which has 4 columns , Firstname, lastname, DOB and Age. How do I go about retrieving just the DOB information for all the rows in the table.
Your help would be most appreciated.
When querying, just specify the columns that you're after:
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] cols = {"dob"};
Cursor result = db.query("person", cols, null, null, null, null, null);
List<String> DobList = new ArrayList<String>();
Cursor cursor = db.rawQuery(String.format("select DOB from %s", TABLE_NAME);
if (cursor.movetoFirst) {
do {
DobList.add(cursor.getString(cursor.getColumnIndex("DOB")));
} while (cursor.moveToNext());
}
cursor.close();