0

Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE users(username TEXT, password TEXT );

public class MyDB extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "users.db";
private static final String TABLE_USERS = "users";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";


public MyDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

// called when database is first created
@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE_TABLE " + TABLE_USERS + "(" +
         //   COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1" +
            COLUMN_USERNAME + " TEXT, " +
            COLUMN_PASSWORD + " TEXT " +
            ");";

    db.execSQL(query);
}

2 Answers 2

3

There is no underscore in the SQL CREATE TABLE statement. Change "CREATE_TABLE " to "CREATE TABLE ".

See also the documentation for CREATE TABLE in SQLite.

Sign up to request clarification or add additional context in comments.

Comments

0

Also, a quick tip. You don't have to necessarily tell SQL that the COLUMN_ID is an INTEGER PRIMARY KEY AUTOINCREMENT. An INTEGER PRIMARY KEY in SQL will always take the value of the ID of the row. The only time when you have to add INCREMENT is if you don't want reused values when you erase data (since, like i said earlier, it takes the ID of the row).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.