简体   繁体   中英

SQLITE Error for Create table

I am getting

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

in Line

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

please help some one....thanks in advance.

You cannot create empty table. At least one column must be specified: https://sqlite.org/lang_createtable.html

Table cannot be empty, try writing some columns as follows:

CREATE TABLE IF NOT EXISTS table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);
 "CREATE TABLE IF NOT EXISTS tb_quiz "
          + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMMENT+ " text");

You should provide the schema of the table.

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

Try this,

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM