在创建SQlite数据库时遇到麻烦,没有这样的列吗?

时间:2018-09-05 22:05:03

标签: android database sqlite

让我首先说,是的,每次重试时,我都会从模拟器中卸载该应用程序!

好吧,希望这很简单。我正在学习android SQLITE,下面是一个非常简单的数据库,其中将文本值设置为default。自然会认为,通过将列设置为默认值,Sqlite应该会自动插入一行吗?不管我一直没有像myColumn错误这样的列。请帮忙!

public class DatabaseTestHelper extends SQLiteOpenHelper{
    private static final String DB_NAME = "DB_NAME";
    private static final int DB_VERSION = 1;

private static final String DATABASE_CREATE =
        "CREATE TABLE SORT_TABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT, 
myColumn TEXT DEFAULT 'IT WORKED!!');";


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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
 }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

这是实例化的onClick按钮

 public void onClickYo(View view) {
    new AsyncTaskTest().execute(true);
}

here is the Async in the same class
  private class AsyncTaskTest extends AsyncTask<Boolean,Void,String>{
        String success;

        @Override
        protected String doInBackground(Boolean... sortTests) {
            try {
                SQLiteOpenHelper dbHelper =
                        new DatabaseTestHelper(titleActivity.this);
                SQLiteDatabase theDb = dbHelper.getReadableDatabase();
                Cursor cursor = theDb.query(
                        "SORT_TABLE",new String[]{"myColumn"},
                        null,null,null,
                        null,null);

                if (cursor.moveToFirst()) {

                     success = cursor.getString(0);
                } else {
                    success = "MOVE TO FIRST TRIPPIN";
                }

                cursor.close();
                theDb.close();

            } catch(SQLiteException e){
                e.printStackTrace();
            }
            return success;
        }

        @Override
        protected void onPostExecute(String dbString) {
            textView.setText(dbString);

        }
    }
}


09-05 18:32:31.935 10341-10387/com.hf.mysquad E/SQLiteLog: (1) no such column: myColumn

1 个答案:

答案 0 :(得分:1)

尝试一下:

  private static final String DATABASE_CREATE = 
         "CREATE TABLE SORT_TABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT,myColumn TEXT);";

  db.execSQL(DATABASE_CREATE);

在问题2中,有五个",双引号的数量是奇数,因此是错误的。

在您的ISSUE#1中,特殊单引号应替换为doulbe单引号,

' shoule be ''

更好的方法是使用专门用于此目的的本机Android方法。 sqlEscapeString