0

This is my first SQLite App and I found a lot of errors and I don't what to do.

Can anyone tell me what I need to do?

This is my log cat:

12-26 18:58:05.870 26185-26185/? I/art: Late-enabling -Xcheck:jni
12-26 18:58:05.889 26185-26185/? E/Environment: initForCurrentUser:userId= 0
12-26 18:58:05.889 26185-26185/? D/Environment: UserEnvironment current 
userId IS : 0
12-26 18:58:05.889 26185-26185/? D/Environment: UserEnvironment PRIMARY 
STORAGE IS : MEDIA_INTERNAL
12-26 18:58:05.982 26185-26185/com.example.acer.sqlitecrud I/LoadedApk: No 
resource references to update in package com.hmct.hmcttheme
12-26 18:58:06.010 26185-26185/com.example.acer.sqlitecrud W/art: Before 
Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-26 18:58:06.135 26185-26185/com.example.acer.sqlitecrud E/SQLiteLog: (1) 
near "TABLEstudentTable": syntax error
12-26 18:58:06.136 26185-26185/com.example.acer.sqlitecrud D/AndroidRuntime: 
Shutting down VM
12-26 18:58:06.138 26185-26185/com.example.acer.sqlitecrud E/AndroidRuntime: 
FATAL EXCEPTION: main

Process: com.example.acer.sqlitecrud, PID: 26185

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.acer.sqlitecrud/com.example.acer.sqlitecrud.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEstudentTable": syntax error (code 1): , while compiling: CREATE TABLEstudentTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:135)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5270)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708)
                                                                          Caused by: android.database.sqlite.SQLiteException: near "TABLEstudentTable": syntax error (code 1): , while compiling: CREATE TABLEstudentTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)
                                                                             at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                             at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                             at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                             at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                             at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                             at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                             at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                             at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                             at com.example.acer.sqlitecrud.DatabaseHelper.onCreate(DatabaseHelper.java:27)
                                                                             at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                             at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                             at com.example.acer.sqlitecrud.DatabaseHelper.<init>(DatabaseHelper.java:22)
                                                                             at com.example.acer.sqlitecrud.MainActivity.onCreate(MainActivity.java:22)
                                                                             at android.app.Activity.performCreate(Activity.java:6075)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2281)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:135) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5270) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708) 
12-26 18:58:06.249 26185-26185/com.example.acer.sqlitecrud I/Process: Sending signal. PID: 26185 SIG: 9

This is my class:

DatabaseHelper.Java

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String dbName = "Student.db";
public static final String tbName = "studentTable";
public static final String colID = "ID";
public static final String colName = "NAME";
public static final String colMarks = "MARKS";

public DatabaseHelper(Context context) {
    super(context, dbName, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE"+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS"+tbName);
    onCreate(db);
}

public boolean insertData (String Name, String Marks){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(colName,Name);
    contentValues.put(colMarks,Marks);
    long result = db.insert(tbName,null,contentValues);

    if (result == -1){
        return false;
    }else {
        return true;
    }
}
}

MainActivity.java:

package com.example.acer.sqlitecrud;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

DatabaseHelper myDb;
EditText Name, Marks;
Button addData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myDb = new DatabaseHelper(this);

    Name = (EditText)findViewById(R.id.edtNama);
    Marks = (EditText)findViewById(R.id.edtNilai);
    addData = (Button)findViewById(R.id.btnInsert);

    InsertData();

}

private void InsertData() {
    addData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isInserted = myDb.insertData(Name.getText().toString(),
                    Marks.getText().toString());

            if (isInserted = true){
                Toast.makeText(MainActivity.this,"Data Telah Di Inputkan !!",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,"Data Gagal Di Inputkan !!",Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}
2
  • I see the question was already answered but, being you are new in developing I recommend you always double check the stack trace. The source of the error was clear there. Anyway, if you works with databases, SQLite or any other, keep a local instance to test your queries. Many times will be of use. Good Luck!! Commented Dec 26, 2017 at 13:23
  • yeah, thank you. i didnt know that it need same space before i put my table name hehe Commented Dec 28, 2017 at 12:16

4 Answers 4

1
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");
}

note the space between the TABLE and tbName

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

Comments

0

Query syntax error, space is missing.

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT,      NAME TEXT, MARKS INTEGER)");
}

Comments

0

You are missing space in syntax of create table

db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");

In drop table also you missed the space

"DROP TABLE IF EXISTS "+tbName

Comments

0

check below code

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)"); //add space after TABLE
}

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.