2

我开发了一个使用 sqlite 数据库的 android 应用程序。

  1. 主要问题: *我一直面临这个错误,没有这样的专栏,但我只是找不到这个问题的原因。任何意见将不胜感激。*
  2. 次要问题: 我希望应用程序检查用户的信息是否在数据库中。如果它不存在,我希望系统能够处理某些事情。检查部分怎么做?我尝试了类似下面的方法。但我不确定它是否有效,因为没有这样的专栏问题。

                    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' 
4

2 回答 2

2

后面少了一个逗号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);");

那应该解决主要问题,在另一个答案中给出了次要问题的解决方案。我认为这很好。

于 2012-12-03T03:01:46.050 回答
1

Primary Issue

也许您首先创建了没有 contentId 的表。然后您添加了新列 contentId。但是表没有重新创建。要重新创建表和 db 再次卸载应用程序,然后再次安装。

Secondary Issue

你可以这样做

Cursor c = myDB.rawQuery("SELECT * from synccontent where emailid_sync='"+emailid_sync+"' ", null);
if(c.getCount()==0){       //not in db
}
于 2012-12-03T02:58:21.590 回答