connecting two tables with foreign keys in sqlite - android

Hi in my senario there is a table with 4 columns and im trying to create another table with connection to the first table but i dont whay im getting this error in logcat
2019-10-28 01:04:00.853 29812-29812/com.test.fastfoodfinder E/SQLiteDatabase: Error inserting notes_main=testeststststststststs
android.database.sqlite.SQLiteException: no such table: notes (code 1 SQLITE_ERROR): , while compiling: INSERT INTO notes(notes_main) VALUES (?)
so i have created a class for my data base and this is what i have done
public class RestaurantDBHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "FastFood_DataBase.db";
private final static int DATABASE_VERSION = 1;
private final static String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_RESTAURANT_ID + " INTEGER PRIMARY KEY ," +
COLUMN_RESTAURANT_NAME + " TEXT, " +
COLUMN_RESTAURANT_ADDRESS + " TEXT, " +
COLUMN_RESTAURANT_TYPE + " INTEGER, " +
COLUMN_RESTAURANT_IMAGE + " INTEGER);";
private final static String CREATE_TABLE_NOTES = "CREATE TABLE " + TABLE_NAME_NOTES +
"(" + COLUMN_NOTES_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_NOTES + " TEXT," + "FOREIGN KEY (" + COLUMN_NOTES_ID + ") REFERENCES " + TABLE_NAME +"(restaurant_id) ON DELETE CASCADE)";
public final static String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
public RestaurantDBHelper(#Nullable Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("PRAGMA FOREIGN_KEYS = ON;");
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE_NOTES);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DELETE_TABLE);
onCreate(db);
}
public void addRestaurant(Restaurant restaurant) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_RESTAURANT_NAME, restaurant.getName());
values.put(COLUMN_RESTAURANT_ADDRESS, restaurant.getAddress());
values.put(COLUMN_RESTAURANT_TYPE,restaurant.getType());
values.put(COLUMN_RESTAURANT_IMAGE, restaurant.getType());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void addNotes (Restaurant restaurant) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NOTES,restaurant.getNote());
db.insert(TABLE_NAME_NOTES,null,values);
db.close();
}
and
public class RestaurantContract {
public static class EntryRestaurants {
public final static String TABLE_NAME = "restaurants";
public final static String COLUMN_RESTAURANT_ID = "restaurant_id";
public final static String COLUMN_RESTAURANT_NAME = "restaurant_name";
public final static String COLUMN_RESTAURANT_ADDRESS = "restaurant_address";
public final static String COLUMN_RESTAURANT_TYPE = "restaurant_type";
public final static String COLUMN_RESTAURANT_IMAGE = "restaurant_image_type";
public final static String COLUMN_RESTAURANT_NOTE_ID = "note_id";
public final static String TABLE_NAME_NOTES = "notes";
public final static String COLUMN_NOTES_ID = "notes_id";
public final static String COLUMN_NOTES = "notes_main";
public final static int RESTAURANT_TYPE_DELIVERY = 1;
public final static int RESTAURANT_TYPE_SITDOWN = 2;
public final static int RESTAURANT_TYPE_TAKEAWAY = 3;
}
}
im kind a new in android so any help would be appreciated,thanks

I believe that your issue is with the onCreate method. This ONLY runs when the database is created, it does not run every time the App is run.
The easiest solution, assuming that you do not need to keep any existing data, is to either delete the App's data or to uninstall the App. After doing either rerun the App and the new table will be created as the onCreate method will then run.
Furthermore it is no use turning FOREIGN KEYS on in the onCreate method. FOREIGN KEYS need to be turned on every time the App is run. To fix this, override the onConfigure method and then use db.setForeignKeyConstraintsEnabled(true);
this is just a convenient alternative to using db.execSQL("PRAGMA FOREIGN_KEYS = ON;");, so if you prefer you could use this when overriding the onConfigure method.
e.g. add this method to the RestaurantDBHelper class :-
#Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
However, you will then have issues when trying to add notes as the child will be set to null and thus their will not be a link/map/association/reference between the added note and the restaurant.
You need to use something like :-
public long addNote(String note, long restaurantId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NOTES,note);
values.put(COLUMN_NOTES_ID,restaurantId);
return db.insert(TABLE_NAME_NOTES,null,values);
}
BUT then you may then have an issue as to determining the id of the restaurant.
BUT then you may then encounter a further issue in that you could only have one note per restaurant as the column used to reference the restaurant is defined as INTEGER PRIMARY KEY and is therefore is a UNIQUE column (the same value can only be used once (an exception is null as a null is considered to be unique to another null)).
If the requirement is for one note per restaurant then there is no need for the separate table the relationship is a one to one relationship so the value can be stored in the restaurant table.
If you want a restaurant to have multiple notes (one to many relationship) then you should not make the column INTEGER PRIMARY KEY, INTEGER would suffice. Then a number of notes could reference the same restaurant.
If you wanted a note to be able to be be applied to a number of restaurants then you'd use a third mapping/line/reference/associative table (other name probably also exist). Such a table would have two columns one to reference the restaurant and the other to reference the note. You would then have a many to many relationship between restaurants and notes (a note could be used by many restaurants and a restaurant could use many notes).
You may find The 3 Types of Relationships in Database Design helpful.

You have enabled foreign key constraints and thus must have a unique primary key for the foreign key to reference.
https://sqlite.org/foreignkeys.html#fk_indexes
says
If the database schema contains foreign key errors that require looking at more than one table definition to identify, then those errors are not detected when the tables are created. Instead, such errors prevent the application from preparing SQL statements that modify the content of the child or parent tables in ways that use the foreign keys. Errors reported when content is changed are "DML errors" and errors reported when the schema is changed are "DDL errors". So, in other words, misconfigured foreign key constraints that require looking at both the child and parent are DML errors. The English language error message for foreign key DML errors is usually "foreign key mismatch" but can also be "no such table" if the parent table does not exist. Foreign key DML errors are reported if:
The parent table does not exist, or
The parent key columns named in the foreign key constraint do not exist, or
The parent key columns named in the foreign key constraint are not the primary key of the parent table and are not subject to a unique constraint using collating sequence specified in the CREATE TABLE, or
The child table references the primary key of the parent without specifying the primary key columns and the number of primary key columns in the parent do not match the number of child key columns.
Given that you never give the restaurant_id a value when you inserting a restaurant then the primary key of the restaurants is probably always null and thus not unique
(Yes according to https://www.sqlitetutorial.net/sqlite-primary-key/ to make the current version of SQLite compatible with the earlier version, SQLite allows the primary key column to contain NULL values. )
So I would say the solution is to create restaurant entries with a unique primary key value when you insert a restaurant or get the database to generate a unique value creating the the restaurant table with the line:-
private final static String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_RESTAURANT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
....
One way to confirm this is download FastFood_DataBase.* files using Device Explorer in your app's database directory and then open it up in https://sqlitebrowser.org/ on your computer to confirm the contents.

Related

android.database.sqlite.SQLiteException: table dataentry has no column named mrsignedname (code 1): , in android [duplicate]

I'm getting this error -
07-03 12:29:18.643: E/SQLiteLog(5181): (1) table accounts has no column named otherNotes
This is my code:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "accountsManager";
private static final String TABLE_ACCOUNTS = "accounts";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_USERID = "userId";
private static final String KEY_PASSWORD = "password";
private static final String KEY_LOGINURL = "loginUrl";
private static final String KEY_OTHERNOTES = "otherNotes";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
+ KEY_USERID + " TEXT," + KEY_PASSWORD + " TEXT," + KEY_LOGINURL + " TEXT,"
+ KEY_OTHERNOTES + " TEXT" + ");";
db.execSQL(CREATE_ACCOUNTS_TABLE);
}
public void addAccount(AccountDetails account) {
SQLiteDatabase db = this.getWritableDatabase();
System.out.println("Hello!");
ContentValues values = new ContentValues();
values.put(KEY_TITLE, account.getTitle()); // Account Title
values.put(KEY_USERID, account.getUserId()); // account userid
values.put(KEY_PASSWORD, account.getPassword()); // account password
values.put(KEY_LOGINURL, account.getLoginUrl()); // account loginurl
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Log.v("title", KEY_TITLE);
// Inserting Row
db.insert(TABLE_ACCOUNTS, null, values);
db.close(); // Closing database connection
}
Also, when I remove the following statement:
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Then I get the same problem with password...etc.
i.e, (1) table accounts has no column named password
Please help!!
It seems that you added some columns later in the database. I do agree with Ken Wolf and you should consider uninstalling and re-installing your app. One better approach is, drop and recreate all tables in onUpdate method, and increase the db version every time you change the schema.
Well, If you are confindent about syntax for creating table, than it may happen
when you add new column in your same table, for that...
1) Unistall from your device and run it again.
OR
2) Setting -> app -> ClearData
OR
3) Change DATABASE_NAME in your "DatabaseHandler" class
( I faced same problem. But I suuceed by changing DATABASE_NAME.)
OR
4) Change DATABASE_VERSION in your "DatabaseHandler" class
(If you have added new column than it will upgrade autimatically)
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
As Benil says in a comment above, change the database version number and run the code again.
The SQL code looks fine.
I think that you forgot to call open() on your database object that you created.
add this methods to you SQL class:
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public DataBaseMain open() throws SQLException{
// Open the database to make her writeable, must be called before writing
// to database
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
// Closing the database for writing, avoids error.
ourHelper.close();
}
And use when you want to call you DB.
Try "Clear Data " of your App from Settings.
For me this problem was occuring because i was storage data on SD Card and later i added some columns.
So if you are saving on SD card delete the previous data.
For those who have similar problem and above solution is not worked then check 2 things:
Space between column name and data type like
COLUMN_NUMBER+" TEXT PRIMARY KEY) not COLUMN_NUMBER+"TEXT PRIMARY KEY)
The order of column during the creation of table should same as map for data insertion like
ContentValues values = new ContentValues();
values.put(TEST_ID, testId);
values.put(CLASS_NAME, className);
values.put(SUBJECT, subject);
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TEST_ID + " INTEGER,"
+ CLASS_NAME + " TEXT,"
+ SUBJECT + " TEXT,"
Here It is your query..TRY this.
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE "+TABLE_ACCOUNTS+"(KEY_ID INTEGER PRIMARY KEY,KEY_TITLE TEXT,KEY_USERID TEXT,KEY_PASSWORD TEXT,KEY_LOGINURL TEXT,KEY_OTHERNOTES TEXT);";
It's possible if you made a mistake in creation query and then fix it,
So in the file system you have a db, but this db possibly has no such column.
Solution:
in emulator find a db file: data/data/com.somecompany.yourapp/databases/db and remove it, then try again.
It's also possible to open this file in some sql explorer and check if there is that column.
I ran into the same issue, as I updated my database table in sqlite with new columns during testing phase.
Besides the answers that were already given above, what I found really useful to update SQLite databases (e.g. after changes) is ABD Idea, an AndroidStudio plugin that allows you to:
Uninstall App
List item
Kill App
Start App
Restart App
Clear App Data
Clear App Data and Restart
You can just specify the version like this:
private static final int VERSION = 4;
If your sure your codes are OKAY, just try uninstalling the application, then rerun it agian. This solved my issue. Hope it helped
the possible solution also (not the best one) to use different version of DB, like:
public static final int DATABASE_VERSION = 2;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
it works for me.
clearing app data from the phone settings worked in my case.
either in the android emulator, you can do the same.
Current code is
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
Now add colmn encrypted
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$encrypted INT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
if (newVersion == 2) {
val query = "ALTER TABLE $tblPassword ADD COLUMN $encrypted INT DEFAULT '0'"
db!!.execSQL(query)
}
}
Here if New user then oncreate method directly create table with encrypted value.
if Old user then onUpgrade method has check db version and alter the table
I know i have replie to this question to late but maybe someone can solve her problem with this answer.
You are put some data later in table database so the best solution is to re name your data base name.
In my case there was a query syntax error (need space b/w column name and datatype ..see attached image) in onCreate() method
to resolve it ..
I followed these steps
Correct your syntax error
change DB version number
Clear App data from settings
Uninstall and Run Your app again.]1]1
Hope it helps
Uninstall your app and reinstall your app and after that it will work fi

creating two tables in android app database

I'm trying to create two tables in my android app database and the application crashes when i'm tring to insert values to the first table, but when i delete the database and recreate it only with one table, it works fine. i don't get it?
the code is:
private static String query_create_user = "CREATE TABLE User ( email TEXT PRIMARY KEY, firstName TEXT, lastName TEXT, password TEXT)";
private static String query_create_group = "CREATE TABLE Group ( groupNumber INTEGER PRIMARY KEY AUTOINCREMENT, courseNumber INTEGER, groupName TEXT, groupType TEXT, groupOwner TEXT)";
public SchoolBagDataBase (Context applicationcontext) {
super(applicationcontext, "contract.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(query_create_user);
database.execSQL(query_create_group);
}
thanks in advance!
The SQL statement held in string 'query_create_group' is invalid. You can't have a table called Group because GROUP is a reserved word in SQLite. Just choose a different name for your table.

Android SQLite issue - table ... has no column named

I'm getting this error -
07-03 12:29:18.643: E/SQLiteLog(5181): (1) table accounts has no column named otherNotes
This is my code:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "accountsManager";
private static final String TABLE_ACCOUNTS = "accounts";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_USERID = "userId";
private static final String KEY_PASSWORD = "password";
private static final String KEY_LOGINURL = "loginUrl";
private static final String KEY_OTHERNOTES = "otherNotes";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
+ KEY_USERID + " TEXT," + KEY_PASSWORD + " TEXT," + KEY_LOGINURL + " TEXT,"
+ KEY_OTHERNOTES + " TEXT" + ");";
db.execSQL(CREATE_ACCOUNTS_TABLE);
}
public void addAccount(AccountDetails account) {
SQLiteDatabase db = this.getWritableDatabase();
System.out.println("Hello!");
ContentValues values = new ContentValues();
values.put(KEY_TITLE, account.getTitle()); // Account Title
values.put(KEY_USERID, account.getUserId()); // account userid
values.put(KEY_PASSWORD, account.getPassword()); // account password
values.put(KEY_LOGINURL, account.getLoginUrl()); // account loginurl
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Log.v("title", KEY_TITLE);
// Inserting Row
db.insert(TABLE_ACCOUNTS, null, values);
db.close(); // Closing database connection
}
Also, when I remove the following statement:
values.put(KEY_OTHERNOTES, account.getOtherNotes()); // account othernotes
Then I get the same problem with password...etc.
i.e, (1) table accounts has no column named password
Please help!!
It seems that you added some columns later in the database. I do agree with Ken Wolf and you should consider uninstalling and re-installing your app. One better approach is, drop and recreate all tables in onUpdate method, and increase the db version every time you change the schema.
Well, If you are confindent about syntax for creating table, than it may happen
when you add new column in your same table, for that...
1) Unistall from your device and run it again.
OR
2) Setting -> app -> ClearData
OR
3) Change DATABASE_NAME in your "DatabaseHandler" class
( I faced same problem. But I suuceed by changing DATABASE_NAME.)
OR
4) Change DATABASE_VERSION in your "DatabaseHandler" class
(If you have added new column than it will upgrade autimatically)
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
As Benil says in a comment above, change the database version number and run the code again.
The SQL code looks fine.
I think that you forgot to call open() on your database object that you created.
add this methods to you SQL class:
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public DataBaseMain open() throws SQLException{
// Open the database to make her writeable, must be called before writing
// to database
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
// Closing the database for writing, avoids error.
ourHelper.close();
}
And use when you want to call you DB.
Try "Clear Data " of your App from Settings.
For me this problem was occuring because i was storage data on SD Card and later i added some columns.
So if you are saving on SD card delete the previous data.
For those who have similar problem and above solution is not worked then check 2 things:
Space between column name and data type like
COLUMN_NUMBER+" TEXT PRIMARY KEY) not COLUMN_NUMBER+"TEXT PRIMARY KEY)
The order of column during the creation of table should same as map for data insertion like
ContentValues values = new ContentValues();
values.put(TEST_ID, testId);
values.put(CLASS_NAME, className);
values.put(SUBJECT, subject);
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TEST_ID + " INTEGER,"
+ CLASS_NAME + " TEXT,"
+ SUBJECT + " TEXT,"
Here It is your query..TRY this.
String CREATE_ACCOUNTS_TABLE = "CREATE TABLE "+TABLE_ACCOUNTS+"(KEY_ID INTEGER PRIMARY KEY,KEY_TITLE TEXT,KEY_USERID TEXT,KEY_PASSWORD TEXT,KEY_LOGINURL TEXT,KEY_OTHERNOTES TEXT);";
It's possible if you made a mistake in creation query and then fix it,
So in the file system you have a db, but this db possibly has no such column.
Solution:
in emulator find a db file: data/data/com.somecompany.yourapp/databases/db and remove it, then try again.
It's also possible to open this file in some sql explorer and check if there is that column.
I ran into the same issue, as I updated my database table in sqlite with new columns during testing phase.
Besides the answers that were already given above, what I found really useful to update SQLite databases (e.g. after changes) is ABD Idea, an AndroidStudio plugin that allows you to:
Uninstall App
List item
Kill App
Start App
Restart App
Clear App Data
Clear App Data and Restart
You can just specify the version like this:
private static final int VERSION = 4;
If your sure your codes are OKAY, just try uninstalling the application, then rerun it agian. This solved my issue. Hope it helped
the possible solution also (not the best one) to use different version of DB, like:
public static final int DATABASE_VERSION = 2;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
it works for me.
clearing app data from the phone settings worked in my case.
either in the android emulator, you can do the same.
Current code is
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
Now add colmn encrypted
override fun onCreate(db: SQLiteDatabase?) {
val createPWDTable =
"CREATE TABLE IF NOT EXISTS $tblPassword ($pwdId INTEGER PRIMARY KEY autoincrement,$title TEXT,$email TEXT,$pwd TEXT,$encrypted INT,$notes TEXT);"
db!!.execSQL(createPWDTable)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
if (newVersion == 2) {
val query = "ALTER TABLE $tblPassword ADD COLUMN $encrypted INT DEFAULT '0'"
db!!.execSQL(query)
}
}
Here if New user then oncreate method directly create table with encrypted value.
if Old user then onUpgrade method has check db version and alter the table
I know i have replie to this question to late but maybe someone can solve her problem with this answer.
You are put some data later in table database so the best solution is to re name your data base name.
In my case there was a query syntax error (need space b/w column name and datatype ..see attached image) in onCreate() method
to resolve it ..
I followed these steps
Correct your syntax error
change DB version number
Clear App data from settings
Uninstall and Run Your app again.]1]1
Hope it helps
Uninstall your app and reinstall your app and after that it will work fi

onCreate not being called after getWritableDatabase/getReadableDatabase

My app's got a database with three tables in it: one to store the names of the people it tracks, one to track an ongoing event, and one - for lack of a better term - for settings.
I load the first table when the app starts. I ask for a readable database to load in members to display, and later I write to the database when the list changes. I've had no problems here.
The other two tables, however, I can't get to work. The code in the helper classes is identical with the exception of class names and column names, and (at least until the point where I try to access the table) the code to use the table is nearly identical as well.
Here's the code for my helper class (I've got a separate helper for each table, and as I said, it's identical except for class names and columns):
public class db_MembersOpenHelper extends SQLiteOpenHelper
{
public static final String TABLE_NAME = "members_table";
public static final String[] COLUMN_NAMES = new String[] {
Constants.KEY_ID,
"name",
"score"
};
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_NAMES[0] + " INTEGER PRIMARY KEY autoincrement, "
+ COLUMN_NAMES[1] + " TEXT, "
+ COLUMN_NAMES[2] + " INTEGER);";
public db_MembersOpenHelper(Context context)
{
super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_CREATE); }
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion + ".");
// Do nothing. We do not have any updated DB version
}
}
Here's how I use it successfully:
db_MembersOpenHelper membersDbHelper = new db_MembersOpenHelper(this);
SQLiteDatabase membersDb = membersDbHelper.getReadableDatabase();
Cursor membersResult = membersDb.query(TABLE_NAME, null, null, null, null, null, null);
members = new HashMap<String, Integer>();
membersResult.moveToFirst();
for(int r = 0; r < membersResult.getCount(); r++)
{
members.put(membersResult.getString(1), membersResult.getInt(2));
membersResult.moveToNext();
}
membersDb.close();
And here's where it fails:
db_PlayersOpenHelper playersDbHelper = new db_PlayersOpenHelper(this);
final SQLiteDatabase playersDb = playersDbHelper.getWritableDatabase();
if(newGame)
{
for(String name : players)
{
ContentValues row = new ContentValues();
row.put(COLUMN_NAMES[1], name);
row.put(COLUMN_NAMES[2], (Integer)null);
playersDb.insert(TABLE_NAME, null, row);
}
}
The first one works like a charm. The second results in ERROR/Database(6739): Error inserting achievement_id=null name=c
android.database.sqlite.SQLiteException: no such table: players_table: , while compiling: INSERT INTO players_table(achievement_id, name) VALUES(?, ?);
...
I did do some testing, and the onCreate method is not being called at all for the tables that aren't working. Which would explain why my phone thinks the table doesn't exist, but I don't know why the method isn't getting called.
I can't figure this out; what am I doing so wrong with the one table that I accidentally did right with the other?
I think the problem is that you are managing three tables with with three helpers, but only using one database. SQLiteOpenHelper manages on database, not one table. For example, it checks to see whether the database, not table, exists when it starts. It already does, so onCreate() does not fire.
I would manage all tables with one helper.
Let me see if I get this right. You are trying to create one database with three tables. But when you create the database, you create just one table; you are somehow instantiating the same database at a different place and wonder why its onCreate method doesn't get called. Is this a correct interpretation?
My strategy would be to try and create all three tables in the single onCreate() method.
If you are working with multiple tables, then you have to create all of the tables at once. If you have run your application first and later you update your database, then it will not upgrade your DB.
Now delete your application, then run it again.
There is one more solution but it is not proper. You can declare onOpen method in which you can call onCreate. And add IF NOT EXISTS before table name in your create table string. – Sourabh just now edit

why does db.insert() giving the sqliteconstraintexception error 19 constraint failed

I have read most of the questions related to this exception but none of them are clear or indicative of why db.insert would throw this error. It was working fine without errors until I manually deleted the db from DDMS. Following is my SQLiteOpenHelper code:
public class LoginSQLiteOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "logincredentials.sqlite";
public static final int DB_VERSION_NUMBER = 1;
public static final String DB_TABLE_NAME = "credentials";
public static final String USERNAME = "user_name";
public static final String PASSWORD = "password";
private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME +
"( _id integer primary key autoincrement," +
USERNAME + " text not null, " +
PASSWORD + " text not null );" ;
public LoginSQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION_NUMBER);
}
#Override
public void onCreate(SQLiteDatabase aSqliteDB) {
Logger.d("Create", "Creating the database...");
aSqliteDB.execSQL(DB_CREATE_SCRIPT);
}
}
My code for inserting the values is:
ContentValues contentValues = new ContentValues();
contentValues.put(LoginSQLiteOpenHelper.USERNAME, loginId);
contentValues.put(LoginSQLiteOpenHelper.PASSWORD, password);
database.insert(LoginSQLiteOpenHelper.DB_TABLE_NAME, null, contentValues);
This is why it occurred to me. If you declare one of your column name type as UNIQUE in your Create Table query in Database and try to insert a non unique variable, it invokes SQLiteConstraintException error.
A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints. For each UNIQUE constraint on the table, each row must feature a unique combination of values in the columns identified by the UNIQUE constraint. As with PRIMARY KEY constraints, for the purposes of UNIQUE constraints NULL values are considered distinct from all other values (including other NULLs). If an INSERT or UPDATE statement attempts to modify the table content so that two or more rows feature identical values in a set of columns that are subject to a UNIQUE constraint, it is a constraint violation. Source - http://www.sqlite.org/lang_createtable.html
I have read pretty much all forums looking for an exact reason for the occurrence of this exception. However, nowhere it clearly states so. However, by means of this code of mine, I can explain why it ocurred for me.
The code snippet I provided, is actually flawless. I am doing exactly what is required to do a db.insert().
However, i figured out the exception in 2 steps.
1. first time when i inserted values, I did not insert a value for the column Password.
2. second time, I added a value for column for Password, but due to incorrect passing of values it was null.
hence, I deduced from this exercise, that no column are allowed null values. You must initialize them with some value.
Please feel free to comment/add or correct me if I am wrong. I would like anyone else running into this issue to be clear on it as there are no good documentation on this exception.

Categories

Resources