创建表

时间:2017-04-19 10:42:02

标签: java android sqlite

我正在

>android.database.sqlite.SQLiteException: near "tb_quiz": syntax error (code 1):

在行

> @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE table IF NOT EXISTS tb_quiz");
    Log.e("--------------", "file table created");
}

请帮助一些......提前谢谢。

5 个答案:

答案 0 :(得分:1)

您无法创建空表。必须至少指定一列:https://sqlite.org/lang_createtable.html

答案 1 :(得分:1)

表不能为空,请尝试按如下方式编写一些列:

CREATE TABLE IF NOT EXISTS table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);

答案 2 :(得分:0)

 "CREATE TABLE IF NOT EXISTS tb_quiz "
          + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMMENT+ " text");

您应该提供表格的架构。

答案 3 :(得分:0)

请使用以下查询创建表格 String CREATE_CLASS_TABLE =“CREATE TABLE class_table(class_id INTEGER,class_name TEXT)”

答案 4 :(得分:0)

试试这个,

public class DataBaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "DataBaseHelper";

    // Database Name
    private static final String DATABASE_NAME = "MYAPP";

    // Database Version
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_USER = "user_info";

    // Common column names TABLE_USER
    private static final String KEY_ID = "id";
    private static final String KEY_USERNAME = "u_name";
    private static final String KEY_PASSWORD = "u_pwd";

    private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_USER
            + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_USERNAME + " TEXT,"
            + KEY_PASSWORD + " TEXT"
            + ")";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // creating required tables
        db.execSQL(CREATE_TABLE_USER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed

        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_USER);
        // Create tables again
        onCreate(db);
    }
}