i am having a DB connection in my application.i used below code
db = openOrCreateDatabase(
"highscore.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
final String CREATE_TABLE_HIGHSCORE =
"CREATE TABLE HIGHSCOREDETAILS ("
+ "NAME TEXT,"
+ "SCORE INTEGER);";
db.execSQL(CREATE_TABLE_HIGHSCORE);
but when the second time i am running the app it crashes...the error showing is
06-13 16:35:51.552: ERROR/AndroidRuntime(1398): Caused by: android.database.sqlite.SQLiteException: table HIGHSCOREDETAILS already exists: CREATE TABLE HIGHSCOREDETAILS (NAME TEXT,SCORE INTEGER);
And i also want to retrieve the greatest value in the score column.This is the first time i am working with DB connection...so please help me...thanks in advance
Change your query to:
CREATE TABLE IF NOT EXISTS HIGHSCOREDETAILS (
To get the highest value use this:
SELECT MAX(SCORE) FROM HIGHSCOREDETAILS
Exception tells you that table HIGHSCOREDETAILS already exists. If you update DB, check if this table deleted previously
Related
I am trying to insert data in database but it is giving me following error.
table places has no column named PLACE_NAME (code 1): , while compiling: INSERT INTO places(PLACE_NAME,IS_SELECTED,placeID,LONGITUDE,LATITUDE) VALUES (?,?,?,?,?)
here is how I am creating my Database Table
// Create a table to hold the places data
final String SQL_CREATE_PLACES_TABLE = "CREATE TABLE IF NOT EXISTS " + PlaceContract.PlaceEntry.TABLE_NAME + " (" +
PlaceContract.PlaceEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + PlaceContract.PlaceEntry.COLUMN_PLACE_NAME + " VARCHAR, " +
PlaceContract.PlaceEntry.COLUMN_PLACE_LATITUDE + " VARCHAR, " + PlaceContract.PlaceEntry.COLUMN_PLACE_LONGITUDE + " VARCHAR, "+
PlaceContract.PlaceEntry.COLUMN_PLACE_IS_SELECTED + " VARCHAR, " +
PlaceContract.PlaceEntry.COLUMN_PLACE_ID + " TEXT NOT NULL, " +
"UNIQUE (" + PlaceContract.PlaceEntry.COLUMN_PLACE_ID + ") ON CONFLICT REPLACE" +
"); ";
please help me what I am doing wrong ....
The most common causes of column not found are typing errors and a misconception in regards to the onCreate method.
The former is unlikely if you are consistently using a single source for the column name e.g. if you use PlaceContract.PlaceEntry.COLUMN_PLACE_NAME to refer to the place_name column.
With the latter, the onCreate method only runs automatically when the database is created, any changes made to the schema, such as adding columns, will not be applied. Thus if you changed the CREATE SQL string to add the PLACE_NAME column that column will not be added.
When developing an App and when the data can be lost then then there are three quick ways to rectify the situation.
Delete the App's data and rerun (the database will be deleted (unless the database is stored outside of the App (not recommended and not the typical scenario))).
Uninstall the App and rerun (also delete's the App's data).
IF the onUpgrade will drop the said table or tables and then recreate the tables (generally by calling the onCreate method) then the database version can be increased (this is the 4th parameter of the super call when constructing the Database Helper class (i.e. the class that extends SQLiteOpenHelper)).
If the data in the database cannot be lost, then the alternative is to use the ALTER to add the column or to create another table, copy the data from the original table and then drop the original table and use ALTER to rename the new table to be the original table.
I'm trying to create an SQLite database in an Android app, but my app keeps crashing and LogCat gives the following error:
FATAL EXCEPTION: main
Process: com.zebra.leadcapture, PID: 15427
android.database.sqlite.SQLiteException: table lead_capture_database has no column named EntryNumber (code 1): , while compiling: INSERT INTO lead_capture_database(EntryNumber,BarcodeData,Notes) VALUES('0','','');
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table lead_capture_database has no column named EntryNumber (code 1): , while compiling: INSERT INTO lead_capture_database(EntryNumber,BarcodeData,Notes) VALUES('0','','');)
Here's the code I'm using:
public void addNewEntry(String barcodeDataString, String notesString) {
leadDatabase = openOrCreateDatabase(database_name,MODE_PRIVATE,null);
String create_string = "CREATE TABLE IF NOT EXISTS " + database_name
+ "(EntryNumber TEXT,BarcodeData TEXT,Notes TEXT);";
String insert_string = "INSERT INTO " + database_name + "(EntryNumber,BarcodeData,Notes) VALUES('" + entryCount + "','" + barcodeDataString + "','" + notesString + "');";
leadDatabase.execSQL(create_string);
leadDatabase.execSQL(insert_string);
entryCount++;
}
Why is it not recognizing EntryNumber as a column?
This was answered in the comments above, but for future reference: I deleted the app from my device and reinstalled it through Android Studio which fixed the problem. I had previously been writing the app with a SQLite database without that column but with the same name. I imagine changing the string I was using for the name would have had the same effect.
You added that column after table was created. In this case, that column is not added into database. If you drop the table and recreate it, it will fix your problem without uninstalling the app. Also you can create a method for migration.
I have a table in an SQLite database on Android that only contains a column with the row ID. This table is used to link datasets in different tables. When I try to insert a new row in the table it throws this exception:
android.database.sqlite.SQLiteException: near "null": syntax error
(code 1): , while compiling: INSERT INTO journeyDetailsTable(null)
VALUES (NULL)
My table is created like this:
private static final String CREATE_TABLE_JOURNEY_DETAIL = "create table " +
TABLE_NAME + " (" + ID_COLUMN + " integer primary key autoincrement);";
And I am trying to insert a new row like this:
parentID = db.insert(ANOTHER_TABLE, null, new ContentValues());
Any ideas?
The docs for SQLiteDatabase.insert(String, String, ContentValues) clearly state that SQL doesn't allow inserting a completely empty row without naming at least one column name. This means that if you do not specify any values, you need to specify a column name in which to insert the NULL into. You should be fine with db.insert(ANOTHER_TABLE, ID_COLUMN, new ContentValues()) to accomplish that.
Regardless, you should probably re-think your database structure as having tables set up like this does not seem effective, maintainable and overseeable, especially in the long run.
Just pass ID_COLUMN as column name in your insert, null isn't a column, but passing null to id will let ID be auto-incremented:
INSERT INTO journeyDetailsTable(ID_COLUMN) VALUES (NULL)
demo
I have couple of tables which will have new data every time the user logs into the application. I have a column KEY_ROWID which is autoIncrement. I want it to always start with 1.
I tried truncating the table before new data is inserted. I get an error:
"android.database.sqlite.SQLiteException: near "TRUNCATE": syntax
error (code 1): , while compiling: TRUNCATE TABLE StaffListTable"
Sugestions much appreciated.
private static final String CREATE_TABLE_STAFFLIST = "CREATE TABLE " + TABLE_STAFFLIST + " ("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_STAFFID + " TEXT NOT NULL, "
+ KEY_STAFFNAME + " TEXT NOT NULL);";
Its very simple, since you are truncating the table values I understand you don't have to secure them or take a back up. In such a case just dropping the table will do the trick.
In onUpdate() method, write
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); //and call onCreate again
onCreate(db);
Let me know if you need anything else. Happy coding. :)
There's no TRUNCATE in sqlite. You can use DROP to delete tables if you really need to.
There are two ways to effect a truncate (which SQLite doesn't have). You can either DROP and then CREATE as per other answers here.
Alternatively you can do a DELETE to empty the table and then remove the entry for the table from sqlite_sequence:
DELETE FROM sqlite_sequence
WHERE name = 'table_name'
sqlite_sequence holds the last used autoincrement value for tables with such columns. Removing the row for a given table will make the autoincrement key start at 1 again.
I am trying to insert data to a new empty table. But I keep getting error (error code 19: constraint failed). I think the problem may caused by 'INTEGER PRIMARY KEY AUTOINCREMENT'. Here is my code:
database.execSQL("CREATE TABLE IF NOT EXISTS contacts (cid INTEGER PRIMARY KEY AUTOINCREMENT, name varchar NOT NULL, user varchar NOT NULL, UNIQUE(user) ON CONFLICT REPLACE)");
...
String sql = "INSERT OR IGNORE INTO contacts ( name , user) VALUES (?, ?)";
database.beginTransaction();
SQLiteStatement stmt = database.compileStatement(sql);
stmt.bindString(1, name);
stmt.bindString(2, entry.getUser());
int i = (int)stmt.executeInsert();
stmt.execute();
stmt.clearBindings();
stmt.close();
// error: 06-11 20:50:42.295: E/DB(12978): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
Anyone knows what wrong with the sql statement? How can I solve this problem? Thanks
I have read few articles on stackoverflow. But cannot find anyone post related to 'insert or replace' + 'INTEGER PRIMARY KEY AUTOINCREMENT'.
I think your code is doing exactly what it is supposed to be doing. Reading over your code it looks like you want it to ignore inserts where there is already something inserted. The error you are receiving is telling you that the insert has failed.
If you use INSERT IGNORE, then the row won't actually be inserted if it results in a duplicate key. But the statement won't generate an
error. It generates a warning instead. These cases include:
Inserting a duplicate key in columns with PRIMARY KEY or UNIQUE
constraints. Inserting a NULL into a column with a NOT NULL
constraint. Inserting a row to a partitioned table, but the values you
insert don't map to a partition. - "INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
I would recommend catching the SQLiteConstraintException or check before inserting to see if the data is already there. If you need some ideas on how to check if data has been inserted let me know, I have had to do this before. Hope this helps.
There is a good begining to end example of SQLite on Android written by Lars Vogella here
Inside there and down a little ways here is the string that he uses for creating a table:
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null);";
I have not tried either his or yours just now but a few things I noticed that differ between his and yours are:
He has no space between the table name and the opening parenthesis
He has a semicolon inside of the SQL string. after the closing parenthesis.
I am not certain if those will fix it for you, but it would probably be a good start.