error occured when I try to insert data into sqlte

bramasta vikana :

So I recently learn how to insert data in android using sqlite , and this error occurred, I don't know why this error occurred, I have tried to reread my book and I don't have any solution from it.

I've reread my book and find the problem on the internet.

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

@Override
public void onCreate(SQLiteDatabase db){
    String CREATE_TABLE = "CREATE TABLE" + TABLE_CONTACTS + "(" + nomber +
            "INTEGER PRIMARYKEY," + namapen + "TEXT," + idPen + "INTEGER," 
  + namaGej + "TEXT," + idGej + "TEXT )";
    db.execSQL(CREATE_TABLE);
  }

The error in logcat :

Process: com.example.lordbramasta.pakar, PID: 20404
    android.database.sqlite.SQLiteException: near "TABLEtb_penyakit": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLEtb_penyakit(nomberINTEGER PRIMARYKEY,namapenTEXT,idPenINTEGER,namaGejTEXT,idGejTEXT )
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:901)

I expected to insert data into sqlite database.

MikeT :

Change :-

public void onCreate(SQLiteDatabase db){
    String CREATE_TABLE = "CREATE TABLE" + TABLE_CONTACTS + "(" + nomber +
            "INTEGER PRIMARYKEY," + namapen + "TEXT," + idPen + "INTEGER," 
  + namaGej + "TEXT," + idGej + "TEXT )";
    db.execSQL(CREATE_TABLE);
  }

to

public void onCreate(SQLiteDatabase db){
    String CREATE_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + nomber +
            " INTEGER PRIMARY KEY," + namapen + " TEXT," + idPen + " INTEGER," 
  + namaGej + " TEXT," + idGej + " TEXT )";
    db.execSQL(CREATE_TABLE);
  }

Then either :-

  • delete the App's data, or
  • uninstall the App

and then rerun the App.

Explanations

The reason why the issue didn't arise until you attempted to insert is because that would have been the first attempt that the database would have been opened. Instantiating the Database Helper doesn't result in the database being created (i.e. the onCreate method being executed, onCreate is only automatically executed when an attempt is made to open the database (implicit with an insert) and the database doesn't actually exist).

The reason why the App's data should be deleted (or the App is uninstalled, which will delete the App's data) is that this deletes the database and thus the onCreate method will run (it doesn't run when the database exists).

The actual error is because CREATE expects the type of what is to be created in your case CREATE TABLE table_name whilst the SQL you have resolved to CREATE TABLEtb_penyakit so you are trying to tell it to create a CREATE TABLEtb_penyakit and to SQlite there is no such thing as a CREATE TABLEtb_penyakit.

Without the other spaces that have been added in the above, the table would have been created BUT the columns would have been :-

enter image description here

The insert would then have failed, as the columns that you expected to exist would not have existed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95830&siteId=1