获取数据库表中的数据如果不存在则插入它,否则返回行id

时间:2012-06-15 05:11:39

标签: android sql sqlite

我有一个表文件包含一个列date_file_creation,我想创建一个包含日期文件创建的表日期,当我插入一个新文件时我检查表日期是否存在我返回行id并插入它作为表文件中的外键,否则我插入一个新的日期并获取其新的行ID以将其插入表文件中。我怎么能这样做?

这是我的尝试:首先我创建一个函数findfile来检查日期是否存在

public int finddate(String date){
    AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
    SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
    Cursor cursor = sqliteDatabase.rawQuery("SELECT _id FROM " + AndroidOpenDbHelper.TABLE_DATE + " where COLUMN_NAME_DATE = date", null);
    startManagingCursor(cursor);
    if (cursor != null ) {
        if  (cursor.moveToFirst()) {
      do {
          int id = cursor.getInt(cursor.getColumnIndex("_id"));
          return id;
      } while (cursor.moveToNext());
        } 
    }
        return 0;

    }

然后在onCreate我这样做

if( resultat==0)
        {
        Filedate date1=new Filedate();
        date1.setfileDate(providedFileDate);
        filedateArrayList.add(date1);
        insertDate(date1);//insert in table date
        newid=finddate(providedFileDate);//get the row id of the new date inserting
        }
        else
            {newid=resultat; //the row id =the result reterned by the finfdate method}

然后我将其插入文件表

这是myDbhelper

public class AndroidOpenDbHelper extends SQLiteOpenHelper {
// Database attributes
public static final String DB_NAME = "file_db";
public static final int DB_VERSION = 1;

//  file Table attributes
public static final String TABLE_FILE = "file_table";
public static final String COLUMN_NAME_FILE_NAME = "file_name_column";
public static final String COLUMN_NAME_FILE_CATEGORY = "file_category_column";
public static final String COLLUMN_NAME_FILE_THEME= "file_theme_column";
public static final String COLLUMN_NAME_FILE_DATE_CREATING = "file_date_creating_column";
public static final String COLLUMN_NAME_FILE_CLOUD = "file_cloud_column";
public static final String COLLUMN_NAME_FILE_DATE_UPLOADING = "file_date_upload_column";
//category table
public static final String TABLE_CATEGORY = "category_table";
public static final String COLUMN_NAME_CATEGORY = "category_column";
public static final String COLLUMN_NAME_CATEGORY_ABREVIATION = "abreviation_column";
//theme table
public static final String TABLE_THEME = "theme_table";
public static final String COLUMN_NAME_THEME = "theme_column";  
public static final String COLLUMN_NAME_THEME_ABREVIATION = "abreviation_column";
//date table
public static final String TABLE_DATE = "date_table";
public static final String COLUMN_NAME_DATE = "date_column";    


public AndroidOpenDbHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {


    // create category table
    String sqlQueryToCreateCategoryTable = "create table if not exists " + TABLE_CATEGORY + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
            + COLUMN_NAME_CATEGORY + " text not null, "
            + COLLUMN_NAME_CATEGORY_ABREVIATION + " text not null);";
    db.execSQL(sqlQueryToCreateCategoryTable);
    //create theme table
    String sqlQueryToCreateThemeTable = "create table if not exists " + TABLE_THEME + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
            + COLUMN_NAME_THEME + " text not null, "
            + COLLUMN_NAME_THEME_ABREVIATION + " text not null);";

    db.execSQL(sqlQueryToCreateThemeTable);
    //table date creation
    String sqlQueryToCreateDateTable = "create table if not exists " + TABLE_DATE + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
            + COLUMN_NAME_DATE + " text not null);";
    db.execSQL(sqlQueryToCreateDateTable);
            //Because this method get executed every time we created an object of this class. 
            //"create table if not exists TABLE_NAME ( BaseColumns._ID integer primary key autoincrement, FIRST_COLUMN_NAME text not null, SECOND_COLUMN_NAME integer not null);"
    String sqlQueryToCreateFileTable = "create table if not exists " + TABLE_FILE + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
                                                                    + COLUMN_NAME_FILE_NAME + " text not 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+"), " 
                                                                    + COLLUMN_NAME_FILE_CLOUD + " text default null,"
                                                                    + COLLUMN_NAME_FILE_DATE_UPLOADING + " text default null);";
            db.execSQL(sqlQueryToCreateFileTable);



}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion == 1 && newVersion == 2){
        // Upgrade the database
    }       
}

}

1 个答案:

答案 0 :(得分:0)

如果对数据库进行了更改,则该表可能尚不存在。我的数据库遇到了同样的问题。如果它具有相同的名称和相同的版本,即使您通过添加表进行更改,也不会创建数据库的新工作版本。

您必须更改版本号并使用更新数据库功能或吹走当前数据库,以便获得新的工作版本。

检查数据库当前是否创建了data_table。您可以通过在命令行中运行“adb shell”来完成此操作。查找数据库并运行sqlite3 [数据库名称]。进入那里后,您可以通过输入“.table”找到哪些表格。如果data_table没有显示,那么它尚未在数据库中创建。


我更多地查看了您的代码。我认为你没有正确构建file_data表。我对用于表示它的SQL语句进行了一些更改,并且最终能够构建数据库。看看这是否有帮助。

String sqlQueryToCreateFileTable = "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+"));";

把它放在AndroidOpenDbHelper中。您还可能需要清除数据库以确保创建新数据库。只有在没有具有相同名称和版本的数据库时,才会运行数据库onCreate()。如果存在具有相同版本和名称的数据库,则不会创建该数据库的另一个副本。


我还注意到你正试图以错误的方式搜索日期。你需要写这样的东西。

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

否则,您的查询只是在COLUMN_NAME_DATE搜索字符串日期的实例,并尝试将其与date_file表中的列进行比较。您当前的SQL语句是查找假设“数据”列中的值等于“COLUMN_NAME_DATE”中的值的行。

添加到SQL语句的变量需要在引号内添加。 "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + " = '" + VALUE_NAME + "';"

希望这有帮助。