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.
Related
I am programming in Android and am trying to make an SQL database to store highscores. In my onCreate method, I have
String query = "CREATE TABLE " + TABLE_SCORES + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_SCORE + " TEXT" + ");";
db.execSQL(query);
where TABLE_SCORES is the table name and the COLUMNS are my columns.
When I run the app, it works the first time and saves, but crashes the second time. In the logs, it says that it was unable to compile my query. I have already tried IF NOT EXISTS but it did not help.
Simply add IF NOT EXISTS after CREATE TABLE!
you should subclass SQLiteOpenHelper, implement public abstract void onCreate (SQLiteDatabase db), and put your create query there. onCreate is called just once, the first time the database is created. This way you can avoid checking if the table exits or not
CREATE TABLE IF NOT EXISTS not working
Note : onCreate called only once when database created and when you change version number then it will call onUpgrade function.So don't worry it will create only once,anyway you may use
CREATE TABLE IF NOT EXIST will create the table if it doesn't exist, or ignore the command if it does.So change
String CREATE_LOGIN_TABLE = "CREATE TABLE LibLogin ( " + "id INTEGER PRIMARY KEY, " + "password TEXT, "+ "status TEXT )";
to
String CREATE_LOGIN_TABLE = "CREATE TABLE IF NOT EXISTS LibLogin ( " + "id INTEGER PRIMARY KEY, " + "password TEXT, "+ "status TEXT )";
onCreate will be called only once when you install your app first time. Onupgrade will be called if you change the version number of your DB, so it won't be executed everytime you run your applicacion (as Giru said) ;)
Take a look to your version number in your constructor and don't change it if you don't want re-create your DB.
why you don't work with online database and use json string for download date. in this way you can create web site and people use the service on pc and not only on android :)
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'm trying to insert a record into a SQLite database table. The table has a Unique constraint on a column "URL" and a primary key auto increment integer "_id".
I'm using insert or replace as my conflict resolution, since I need the new data to be persisted. However, this method is incrementing the primary key and messing up some foreign keys in other tables.
What's the best way to overwrite the record without updating the _id?
The simple answer is to stop using Replace syntax. This causes the old record to be deleted then a new one added ... which would increment your index.
Utilize the UPDATE syntax to handle conflicts instead
EDIT:
If you are really partial to the Replace syntax then it will come at a cost. You will need to write additional code that updates all prev occurrences of the old index to the new one. Not overly hard but this will correct the issue of synchronizing indexes
Documentation [Listed under REPLACE section little ways down the page]: http://www.sqlite.org/lang_conflict.html
this is my code of SQLite
"CREATE TABLE IF NOT EXISTS posts (" +
"_id integer NOT NULL," +
"id_language integer NOT NULL" +
");" +
"CREATE UNIQUE INDEX posts_idx ON posts(_id, id_language);";
"INSERT OR REPLACE INTO " + DB_TABLE + " (" + formulateColumns() + ") " +
"VALUES (" + formulateValues(v) + ");");
I can look up records by values and/or ID
I can add records
I can delete All records
I can delete a specific record (by either value or ID)
But, the record ID only increments - it never adjusts to reflect the number of remaining records after deleting a record.
I haven't found anything on this subject (not even in the Doc's)
What/How do I do this?
Thanks
The declaration for this part of the db follows...
String sql =
"create table "
+ TABLE
+ "( " + BaseColumns._ID
+ " integer primary key autoincrement, "
+ THE_NUMBER + " integer, "
+ FAV_NAME + " text not null);";
Rather than deleting this post, I'll post the ref to the answer (it's at SQL's site).
link text