Question

I'm working on my first SQLite database app and because I'm stuck for a long time I decided to ask about it online.

I have trouble with running the application I that have written, because of the function: Helper.getWritableDatabase();

without it, it runs fine.

My database indeed created when I take a look at the Data/Data/projects source at the DDMS

Code:

The Symptom class:

public class Symptom {

long symptomId;
String name;
long details;


public Symptom(long symptomId, String name, long details) {
    super();
    this.symptomId = symptomId;
    this.name = name;
    this.details = details;
}

public Symptom() {}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public long getDetails() {
    return details;
}

public void setDetails(long details) {
    this.details = details;
}

public long getSymptomId() {
    return symptomId;
}

public void setSymptomId(long symptomId) {
    this.symptomId = symptomId;
};

}

The SymptomOpenHelper class:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SymptomOpenHelper extends SQLiteOpenHelper {


public static final String DATABASENAME="Symptom.db";
public static final String TABLE_SYMPTOM="tblSymptom";
public static final int DATABASEVERSION = 1;

public static final String COLUMN_ID="symptomId";
public static final String COLUMN_NAME="name";
public static final String COLUMN_DETAILS="details";

private static final String CREATE_TABLE_SYMPTOM="CREATE TABLE IF NOT EXIST" + TABLE_SYMPTOM + "(" + COLUMN_ID + "INTEGER PRIMART KEY AUTOINCREMENT," + COLUMN_NAME +"VARCHAR," +COLUMN_DETAILS +"VARCHAR" + ");";


public SymptomOpenHelper(Context context)
{
    super(context, DATABASENAME, null, DATABASEVERSION);

}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_SYMPTOM);
    Log.i("data" ,"Table sypmpton created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int NewVersion) {
    db.execSQL("DROP TABLE IF NOT EXIST" + TABLE_SYMPTOM);
    onCreate(db);

}

}

The MainActivity code:

    import android.os.Bundle;
    import android.app.Activity;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.Menu;

    public class MainActivity extends Activity {

SymptomOpenHelper sHelper;
SQLiteDatabase database;

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


    sHelper =new SymptomOpenHelper(this);
            // The trouble-maker line:

    database=sHelper.getWritableDatabase(); 

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

According to the android simulator: "Unfortunately, The_app_name has stopped"

The LogCat:

12-31 19:55:37.744: E/Trace(924): error opening trace file: No such file or directory (2) 12-31 19:55:38.964: E/SQLiteLog(924): (1) near "AUTOINCREMENT": syntax error 12-31 19:55:38.994: D/AndroidRuntime(924): Shutting down VM 12-31 19:55:38.994: W/dalvikvm(924): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 12-31 19:55:39.124: E/AndroidRuntime(924): FATAL EXCEPTION: main 12-31 19:55:39.124: E/AndroidRuntime(924): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idiagnostician1/com.example.idiagnostician1.MainActivity}: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS tblSymptom (symptomId INTEGER PRIMART KEY AUTOINCREMENT,name VARCHAR, details VARCHAR); 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.os.Handler.dispatchMessage(Handler.java:99) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.os.Looper.loop(Looper.java:137) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.ActivityThread.main(ActivityThread.java:5041) 12-31 19:55:39.124: E/AndroidRuntime(924): at java.lang.reflect.Method.invokeNative(Native Method) 12-31 19:55:39.124: E/AndroidRuntime(924): at java.lang.reflect.Method.invoke(Method.java:511) 12-31 19:55:39.124: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-31 19:55:39.124: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-31 19:55:39.124: E/AndroidRuntime(924): at dalvik.system.NativeStart.main(Native Method) 12-31 19:55:39.124: E/AndroidRuntime(924): Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS tblSymptom (symptomId INTEGER PRIMART KEY AUTOINCREMENT,name VARCHAR, details VARCHAR); 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594) 12-31 19:55:39.124: E/AndroidRuntime(924): at com.example.idiagnostician1.SymptomOpenHelper.onCreate(SymptomOpenHelper.java:30) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 12-31 19:55:39.124: E/AndroidRuntime(924): at com.example.idiagnostician1.MainActivity.onCreate(MainActivity.java:20) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.Activity.performCreate(Activity.java:5104) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 12-31 19:55:39.124: E/AndroidRuntime(924): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 12-31 19:55:39.124: E/AndroidRuntime(924): ... 11 more 12-31 19:55:44.334: E/Trace(986): error opening trace file: No such file or directory (2) 12-31 19:55:44.624: E/SQLiteLog(986): (1) near "AUTOINCREMENT": syntax error 12-31 19:55:44.624: D/AndroidRuntime(986): Shutting down VM 12-31 19:55:44.634: W/dalvikvm(986): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 12-31 19:55:44.653: E/AndroidRuntime(986): FATAL EXCEPTION: main 12-31 19:55:44.653: E/AndroidRuntime(986): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idiagnostician1/com.example.idiagnostician1.MainActivity}: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS tblSymptom (symptomId INTEGER PRIMART KEY AUTOINCREMENT,name VARCHAR, details VARCHAR); 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.os.Handler.dispatchMessage(Handler.java:99) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.os.Looper.loop(Looper.java:137) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.ActivityThread.main(ActivityThread.java:5041) 12-31 19:55:44.653: E/AndroidRuntime(986): at java.lang.reflect.Method.invokeNative(Native Method) 12-31 19:55:44.653: E/AndroidRuntime(986): at java.lang.reflect.Method.invoke(Method.java:511) 12-31 19:55:44.653: E/AndroidRuntime(986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-31 19:55:44.653: E/AndroidRuntime(986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-31 19:55:44.653: E/AndroidRuntime(986): at dalvik.system.NativeStart.main(Native Method) 12-31 19:55:44.653: E/AndroidRuntime(986): Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS tblSymptom (symptomId INTEGER PRIMART KEY AUTOINCREMENT,name VARCHAR, details VARCHAR); 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594) 12-31 19:55:44.653: E/AndroidRuntime(986): at com.example.idiagnostician1.SymptomOpenHelper.onCreate(SymptomOpenHelper.java:30) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 12-31 19:55:44.653: E/AndroidRuntime(986): at com.example.idiagnostician1.MainActivity.onCreate(MainActivity.java:20) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.Activity.performCreate(Activity.java:5104) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 12-31 19:55:44.653: E/AndroidRuntime(986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 12-31 19:55:44.653: E/AndroidRuntime(986): ... 11 more

Was it helpful?

Solution

ComponentInfo{com.example.idiagnostician1/com.example.idiagnostician1.MainActivity}: android.database.sqlite.SQLiteException: near "EXISTtblSymptom": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTtblSymptom(symptomIdINTEGER PRIMART KEY 

Based on this information in you logcat, you have problem in your query CREATE_TABLE_SYMPTOM. You need to append space after NOT EXIST . example:

"CREATE TABLE IF NOT EXIST " + TABLE_SYMPTOM ...

instead of

"CREATE TABLE IF NOT EXIST" + TABLE_SYMPTOM 

check your query correctly and you will see you have made this mistake all over the queries. Add spaces in other required places as well in same query. Try this query :

"CREATE TABLE IF NOT EXISTS " + TABLE_SYMPTOM + " ("+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME +" VARCHAR, " +COLUMN_DETAILS +" VARCHAR" + ");"

OTHER TIPS

It's probably because CREATE_TABLE_SYMPTOM is wrong

For

COLUMN_ID + "INTEGER PRIMART KEY AUTOINCREMENT," 

You need add a space:

COLUMN_ID + " INTEGER PRIMART KEY AUTOINCREMENT,"

The CREATE_TABLE_SYMPTOM should changed as below:

private static final String CREATE_TABLE_SYMPTOM="CREATE TABLE IF NOT EXISTS " + TABLE_SYMPTOM + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME +" VARCHAR, " +COLUMN_DETAILS +" VARCHAR" + ");";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top