it keeps telling me column _id doesn't exist and in my whole code I didn't find the word _id
public static final String TABLE_NAME = "LIST";
public static final String _ID = "_Id";
public static final String PRODUCT_NAME = "Product";
static final String DB_NAME = "LIST.DB";
static final int DB_VERSION = 1;
private SQLiteDatabase database;
private static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + PRODUCT_NAME + " TEXT NOT NULL );";
final String[] from = new String[]
{DatabaseHelper. _ID , DatabaseHelper. PRODUCT_NAME};
final int[] to = new int[]
{R.id._Id , R.id.Product};
adapter = new SimpleCursorAdapter(this ,R.layout.viewrecord , c , from , to , 0);
listView.setAdapter(adapter);
Related
I am new to SQLite queries in android. Trying to run a query. However, app keeps crashing with the following error :
Caused by: java.lang.IllegalStateException: Couldn't read row 1, col
-1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Below is the extract of Code
public String getOutputData(){
String inputData = "'Vineet'";
Cursor cursorNew = db.rawQuery("SELECT email FROM groupseries INNER JOIN email " +
"ON groupseries.emailid = email._id INNER JOIN user ON " +
"groupseries.userid = user._id WHERE name=" + inputData, null);
cursorNew.moveToFirst ();
StringBuffer buffer = new StringBuffer();
while(cursorNew.moveToNext()){
int index1 = cursorNew.getColumnIndex(DatabaseHelper.KEY_NAME);
int index2 = cursorNew.getColumnIndex(DatabaseHelper.KEY_EMAIL);
String userID = cursorNew.getString(index1);
String emailID = cursorNew.getString(index2);
buffer.append(userID + " " + emailID + "\n");
}
return buffer.toString();
}
Below is the code for table creation I am using. (Open to suggestions to make this less clumsy).
private static final String DATABASE_NAME = "tryDemoDataBase.db";
private static final String TABLE_NAME_USER = "user";
private static final String TABLE_NAME_EMAIL = "email";
private static final String TABLE_NAME_GROUP = "groupseries";
private static final int DATABASE_VERSION = 12;
private static final String KEY_ROWID_USER = "_id";
private static final String KEY_ROWID_EMAIL = "_id";
private static final String KEY_ROWID_GROUP = "_id";
private static final String REFERENCE_USER_ID = "userid";
private static final String REFERENCE_EMAIL_ID = "emailid";
private static final String KEY_NAME="name";
private static final String KEY_EMAIL = "email";
private static final String CREATE_TABLE_USER = "create table "+ TABLE_NAME_USER
+ " ("+ KEY_ROWID_USER+" integer primary key autoincrement, "
+ KEY_NAME + " text)";
private static final String CREATE_TABLE_EMAIL = "create table "+ TABLE_NAME_EMAIL
+ " ("+ KEY_ROWID_EMAIL+" integer primary key autoincrement, "
+ KEY_EMAIL + " text)";
private static final String CREATE_TABLE_GROUP = "create table " + TABLE_NAME_GROUP+ " ("
+ KEY_ROWID_GROUP+" integer primary key autoincrement, "
+ REFERENCE_USER_ID + " integer, " + REFERENCE_EMAIL_ID + " integer)";
First remove this line:
cursorNew.moveToFirst();
because later you use:
while (cursorNew.moveToNext())
so actually you are skipping the 1st (and maybe the only) row of the results.
Then you must include in the selected columns the column name if you want to retrieve it from the Cursor object:
Cursor cursorNew = db.rawQuery("SELECT name, email FROM groupseries INNER JOIN email " +
"ON groupseries.emailid = email._id INNER JOIN user ON " +
"groupseries.userid = user._id WHERE name=" + inputData, null);
And a suggestion to use the recommended and safe way of passing parameters to a query instead of concatenating them directly.
Use ? placeholders:
String inputData = "Vineet"; // no quotes
Cursor cursorNew = db.rawQuery(
"SELECT name, email FROM groupseries INNER JOIN email " +
"ON groupseries.emailid = email._id INNER JOIN user ON " +
"groupseries.userid = user._id WHERE name=?",
new String[] {inputData}
);
I try to read values out of my database, by looking for their _id.
private static final String DATABASE_NAME = "database.db";
private static final String TABLE_NAME = "database";
private static final String COL1 = "_id";
private static final String COL2 = "Name";
public String getData(int id, String col){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT value FROM " + TABLE_NAME +" WHERE
/* id = _id and col = COL2 */", null);
}
You have to change the query a little bit.
"SELECT * FROM " + TABLE_NAME + " WHERE " + COL1 + "=" + id
Replace it with the String you are passing to the method rawQuery()
above solution will work but this might not be the best way to use rawQuery() method, as we are not using it's second parameter selectionArgs. Please refer official documentation for details.
So final solution will look something like this:
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COL1 + "=?", new String[] { String.valueOf(id) });
This question already has answers here:
What's the best way to iterate an Android Cursor?
(10 answers)
Closed 6 years ago.
I have an SQLite Database.
Here's some of the code setting it up:
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DESCRIPTION = "description";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_DESCRIPTION};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_NAME = 1;
public static final int COL_DESCRIPTION = 2;
// DataBase info:
public static final String DATABASE_NAME = "dbMetrics";
public static final String DATABASE_TABLE = "mainMetrics";
public static final int DATABASE_VERSION = 1;
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_DESCRIPTION + " TEXT"
+ ");";
I want to output one of my columns KEY_NAME as an ArrayList.
To do this, I have so far generated the following code:
public ArrayList<String> getAllStringValues() {
ArrayList<String> myStringValues = new ArrayList<String>();
Cursor result = db.query(true, DATABASE_TABLE,
new String[] {KEY_NAME}, null, null, null, null,
null, null);
**********CODE NEEDED HERE***********
System.out.println(Arrays.toString(myStringValues.toArray()));
return myStringValues ;
}
However, I am not sure how I get the data from the column into an ArrayList from here.
My Question
Can someone give me some assistance as to how to design a loop that will go through each row in the column and put that data in an ArrayList?
I'm pretty sure I will need to at some point use
result.getString(result.getColumnIndex(KEY_NAME))
in order to do this, but again, I'm not sure how.
Any help would be greatly appreciated.
if (Cursor.moveToFirst()) {
do {
keyname.add(Cursor.getString(Cursor.getColumnIndex("keyname")));
key_description.add(Cursor.getString(Cursor.getColumnIndex("key_description")));
} while (Cursor.moveToNext());
}
Cursor.close();
try this code if doesnt work, tell me
This question already has answers here:
When does SQLiteOpenHelper onCreate() / onUpgrade() run?
(15 answers)
Closed 7 years ago.
I'm trying to update a particular row of a database, and I have the following code
public String CREATE_QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME +
"(" + TableData.TableInfo.USER_NAME + " TEXT, " + TableData.TableInfo.USER_PIN +
" TEXT, " + TableData.TableInfo.PARTNER_FIRST + " Text, " + TableData.TableInfo.PARTNER_SECOND +
" TEXT, " + TableData.TableInfo.DATE + " TEXT, " + TableData.TableInfo.SIGNATURE_IMAGE + " BLOB, " +
TableData.TableInfo.PARTNER_SIGNATURE + " BLOB);";
And I am getting an error from
ContentValues cv = new ContentValues();
DatabaseOperations DOP = new DatabaseOperations(ctx);
Cursor CR = DOP.getInformation(DOP);
CR.moveToLast();
SQLiteDatabase SQ = DOP.getWritableDatabase();
ContentValues args = new ContentValues();
args.put(TableData.TableInfo.USER_NAME, CR.getString(0));
args.put(TableData.TableInfo.PARTNER_FIRST, partner_name);
SQ.update(TableData.TableInfo.TABLE_NAME, args, "ROWID=?" + id, null);
This is the error:
Caused by: android.database.sqlite.SQLiteException: no such column: partner_first (code 1): , while compiling: UPDATE reg_info SET user_name=?,partner_first=? WHERE ROWID=?54
Here is my table info
public static abstract class TableInfo implements BaseColumns {
public static final String USER_NAME = "user_name";
public static final String USER_PIN = "user_pin";
public static final String PARTNER_FIRST = "partner_first";
public static final String PARTNER_SECOND = "partner_second";
public static final String DATE = "date";
//public static final String LOC = "location";
public static final String SIGNATURE_IMAGE = "signature_image";
public static final String PARTNER_SIGNATURE = "partner_signature";
public static final String DATABASE_NAME = "user_info";
public static final String TABLE_NAME = "reg_info";
}
What am I doing wrong?
Uninstall and rename the database.. This happens frequently when you
change your table name or any change in database.
The problem is in your UPDATE statement - you have a concatenated string and are not passing the "WHERE" arg correctly on this line:
SQ.update(TableData.TableInfo.TABLE_NAME, args, "ROWID=?" + id, null);
simply do this:
SQ.update(TableData.TableInfo.TABLE_NAME, args, "ROWID=" + id, null);
the error is misleading because the SQL statement is wrong altogether.
Why is that when i have more than 3 columns the app crashes? I tried debugging it by commenting out one column and it runs perfectly. After debugging it some more I found out that the problem might be on the 'populateListview' because I was able to run the app but now it won't display anything. Why do you think I couldn't run it with more than 3 columns?
Here's my dbAdapter:
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_INGREDIENTNAME = "ingredientname";
public static final String KEY_IMAGE = "image";
public static final String KEY_DETAILS = "details";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_INGREDIENTNAME, KEY_IMAGE, KEY_DETAILS};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_INGREDIENTNAME = 1;
public static final int COL_IMAGE = 2;
public static final int COL_DETAILS = 3;
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT"
+ KEY_DETAILS + " TEXT"
+ ");";
// Add a new set of values to be inserted into the database.
public long insertRow(String ingredientname, String image, String detailsValue) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_INGREDIENTNAME, ingredientname);
initialValues.put(KEY_IMAGE, image);
initialValues.put(KEY_DETAILS, detailsValue);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
Here's my populateListView Code:
private void populateListView() {
Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[] { //DBAdapter.KEY_ROWID,
DBAdapter.KEY_INGREDIENTNAME };
int[] toViewIDs = new int[] { //R.id.textViewItemNumber,
R.id.textViewItemTask };
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.item_layout, cursor, fromFieldNames, toViewIDs, 0);
ListView myList = (ListView) findViewById(R.id.listViewTask);
myList.setAdapter(myCursorAdapter);
}
You miss a comma, here:
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT"
+ KEY_DETAILS + " TEXT"
+ ");";
It should be
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT,"
+ KEY_DETAILS + " TEXT"
+ ");";