我正在开发一个使用sqlite数据库的android应用程序。
次要问题: 我希望应用程序检查用户的信息是否在数据库内。如果它不存在,我希望系统能够处理某些事情。怎么做检查部分?我试过下面的东西。但由于没有这样的专栏问题,我不确定它是否有效。
String values[] = helper.get_synccontentByEmailID(SettingConstant.EMAIL);
if(!(values[0] == null)){
}
- 数据库类 -
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE synccontent (" +
"tableid INTEGER PRIMARY KEY AUTOINCREMENT, " +
"emailid_sync TEXT " +
"contentid TEXT," +
"contentpath TEXT," +
"filesize TEXT," +
"createdtime TEXT,"+
"createdate TIMESTAMP default current_timestamp);");
}
public String[] get_synccontentByEmailID(String emailid_sync){
SQLiteDatabase myDB = null;
String[] returnMsg = null;
myDB = this.getReadableDatabase();
Cursor c = myDB.rawQuery("SELECT tableid, emailid_sync, contentid, contentpath, filesize, createdtime" +
" from synccontent where emailid_sync='"+emailid_sync+"' ", null);
int emailid_syncColumn = c.getColumnIndex("emailid_sync");
int contentidColumn = c.getColumnIndex("contentid");
int contentpathColumn = c.getColumnIndex("contentpath");
int filesizeColumn = c.getColumnIndex("filesize");
int createdtimeColumn = c.getColumnIndex("createdtime");
if (c.moveToFirst()) {
do {
returnMsg = new String[5];
String contentid = c.getString(contentidColumn);
String contentpath = c.getString(contentpathColumn);
String filesize = c.getString(filesizeColumn);
String createdtime = c.getString(createdtimeColumn);
returnMsg[0] = emailid_sync;
returnMsg[1] = contentid;
returnMsg[2] = contentpath;
returnMsg[3] = filesize;
returnMsg[4] = createdtime;
} while (c.moveToNext());
}
- 主要活动 -
syncButton = (Button) findViewById(R.id.sync_btn);
syncButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String values[] = helper.get_synccontentByEmailID(SettingConstant.EMAIL);
if(!(values[0] == null)){
}
E/AndroidRuntime(4543): android.database.sqlite.SQLiteException: no such column: contentid: , while compiling: SELECT tableid, emailid_sync, contentid, contentpath, filesize, createdtime from wbm_synccontent where emailid_sync='testing@yahoo.com'
答案 0 :(得分:1)
Primary Issue
也许您首先创建了没有contentId的表。然后添加了新列contentId.But表没有重新创建。要重新创建表,db再次卸载应用程序,然后重新安装。
Secondary Issue
你可以这样做
Cursor c = myDB.rawQuery("SELECT * from synccontent where emailid_sync='"+emailid_sync+"' ", null);
if(c.getCount()==0){ //not in db
}
答案 1 :(得分:1)
emailid_sync TEXT
之后缺少逗号。
db.execSQL("CREATE TABLE synccontent (" +
"tableid INTEGER PRIMARY KEY AUTOINCREMENT, " +
"emailid_sync TEXT, " +
"contentid TEXT," +
"contentpath TEXT," +
"filesize TEXT," +
"createdtime TEXT,"+
"createdate TIMESTAMP default current_timestamp);");
这应该解决主要问题,在另一个答案中给出了二级解决方案。我认为没关系。