Skip to content Skip to sidebar Skip to footer

Fetch Data In Database Table Insert It If Not Exist Else Return The Row Id

I have a table file witch contain a column date_file_creation,I would like to create a table date contain the date file creation, when I insert a new file I check the table date if

Solution 1:

If you made changes to you database then the table might not yet exist. I had the same issues with my database. When it has the same name and same version it does not create a new working version of the database even if you make changes by adding tables.

You have to either change the version number and use the update database function or blow away your current database so that you can get the new working version.

Check to see if you database currently has a data_table created. You can do this by running "adb shell" in the command line. Finding your database and running sqlite3 [database name]. Once in there you can find out what tables are there by typing ".table". If data_table does not show up then it has not been created in the database.


I did some more looking at your code. I do not think you file_data table was being built correctly. I made some changes to the SQL statement that you use to exicute it and was finaly able to build the database. See if this helps.

StringsqlQueryToCreateFileTable="create table if not exists " + TABLE_FILE + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
    + COLUMN_NAME_FILE_NAME + " text not null, " + COLLUMN_NAME_FILE_THEME + " text not null,  " + COLLUMN_NAME_FILE_DATE_CREATING + " text not null, "+ COLUMN_NAME_FILE_CATEGORY + " text not null, "
    + COLLUMN_NAME_FILE_CLOUD + " text not null, "+ COLLUMN_NAME_FILE_DATE_UPLOADING + " text default null, FOREIGN KEY (" + COLUMN_NAME_FILE_CATEGORY+") REFERENCES "+TABLE_CATEGORY+" ("+BaseColumns._ID+"), " 
    + " FOREIGN KEY ("+COLLUMN_NAME_FILE_THEME+") REFERENCES "+TABLE_THEME+" ("+BaseColumns._ID+"), " 
    + " FOREIGN KEY ("+COLLUMN_NAME_FILE_DATE_CREATING +") REFERENCES "+TABLE_DATE+" ("+BaseColumns._ID+"));";

Put it in you AndroidOpenDbHelper. You also might have to blow away your database to make sure that the new database is created. The database onCreate() will only run if there is not a database with the same name and version. If a database exists with the same version and name it will not create another copy of the database.


Also I just noticed that you are trying to search for date in the wrong way. you will need to write something like this.

Cursorcursor= sqliteDatabase.rawQuery("SELECT _id FROM " + AndroidOpenDbHelper.TABLE_DATE + " where " + AndroidOpenDbHelper.COLUMN_NAME_DATE + "= '" +date+ "';", null);

Otherwise your query is just searching the COLUMN_NAME_DATE for an instance of the string date and trying to compare it to a column in the date_file table. Your current SQL statement is saying find the rows where the value in the hypothetical "data" column is equal to the value in the "COLUMN_NAME_DATE".

Variables that are added to an SQL statement need to be added inside quotes. "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = '" + VALUE_NAME + "';"

Hope this helps.

Post a Comment for "Fetch Data In Database Table Insert It If Not Exist Else Return The Row Id"