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.
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 have to insert sequential number from 1 to N in an empty column in a database. Android Studio is giving me the following error when I run my code:
Caused by: android.database.sqlite.SQLiteException: unrecognized token: ":" (code 1): , while compiling: SELECT #i:=0; UPDATE table_name SET column_one = #i:=#i+1;
My line of code:
db.execSQL("SELECT #i:=0; UPDATE table_name SET column_one = #i:=#i+1;");
(Please note when I change MySQL statement between parenthesis the code runs and gets executed)
Source of SQL code:
Insert sequential number in MySQL
I'm new in Java, Android Studio and MySQL.
What am I missing here?
Help me to understand why MySQL #i:=#i+1 expression is not running properly in Android Studio.
Thanks in advance.
Because android database is using sqlite,but sqlite dose not supported “#”,if you want batch processing ,I suggest you use Java
Try this .
database.execSQL("SELECT " + #i + " :=0 ; UPDATE table_name SET column_one = " + #i + " := " + #i "+1;");
Error:
android.database.sqlite.SQLiteException: near "-": syntax error (code 1): , while compiling: CREATE TABLE new_info(user-name TEXT,user_mob TEXT,user_email TEXT);
Code:
public String CREATE_QUERY="CREATE TABLE "+ UserContract.addnew.TABLE_NAME+"("+ UserContract.addnew.USER_NAME+" TEXT,"
+ UserContract.addnew.MOBILE+" TEXT,"+ UserContract.addnew.EMAIL+" TEXT);";
As you can see in the query it tries to execute: CREATE TABLE new_info(user-name TEXT,user_mob TEXT,user_email TEXT); You have - instead of _. Just replace user-name with user_name.
Check this post to understand how to be able to use hyphen in the table names.
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.
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