0

I am using datbase to store group details and contact info in the database,but whenever i insert some data in to database i get error saying "no such table".What wrong i have done??

Code

public class GroupDataBase extends SQLiteOpenHelper {

    private static final int dbVersion = 1;
    private static final String dbName = "HSsuraksha";
    private static final String tableName = "groupDetails";
    private static final String groupId = "groupId";
    private static final String groupName = "groupName";
    private static final String createdOn = "createdOn";
    private static final String createTable = "CREATE TABLE " + tableName + "(" + groupId + " Integer Primary Key Auto Increment," + groupName + " Text," + createdOn + " Text" + ")";


    public GroupDataBase(Context context) {
        super(context, dbName, null, dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(createTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

    }


    public void insertGroupDetails(GroupModel groupModel) {
        SQLiteDatabase database = getWritableDatabase();
        database.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put(groupName, groupModel.getGroupName());
        contentValues.put(createdOn, groupModel.getGroupCreatedDate());
        if (contentValues != null) {

            Long id = database.insert(tableName, null, contentValues);
            Log.e("Group insert values", "" + id);

        }
        database.setTransactionSuccessful();
        database.endTransaction();
        database.close();

    }


}

LOGCAT

08-01 12:57:26.129    2522-2522/example.com.pocketdocs E/SQLiteLog﹕ (1) no such table: groupDetails
08-01 12:57:26.129    2522-2522/example.com.pocketdocs E/SQLiteDatabase﹕ Error inserting createdOn=2014-08-01 groupName=test
    android.database.sqlite.SQLiteException: no such table: groupDetails (code 1): , while compiling: INSERT INTO groupDetails(createdOn,groupName) VALUES (?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
            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.insertWithOnConflict(SQLiteDatabase.java:1475)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1347)
            at example.com.pocketdocs.DataBase.GroupDataBase.insertGroupDetails(GroupDataBase.java:50)
            at example.com.pocketdocs.Group.CreateNewGroup.onClick(CreateNewGroup.java:87)
            at android.view.View.performClick(View.java:4147)
            at android.view.View$PerformClick.run(View.java:17161)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
08-01 12:57:26.129    2522-2522/example.com.pocketdocs E/Group insert values﹕ -1

Do i have written the create table query wrong????

user3844417
  • 139
  • 1
  • 4
  • 12

3 Answers3

1

You have a syntax error in your CREATE TABLE: There should be no space between auto and increment.

Since you're not seeing an exception about it, your onCreate() has not been run in its current form.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
1

instead of your table creation query use following

private static final String createTable = "CREATE TABLE " + tableName + "(" + groupId + " Integer Primary Key Autoincrement," + groupName + " Text," + createdOn + " Text" + ")";

i think problem in "auto increment" it should be together.

kamlesh
  • 154
  • 8
0

change your line to this

private static final String createTable = "CREATE TABLE " + tableName + "(" + groupId + " Integer Primary Key Autoincrement," + groupName + " Text," + createdOn + " Text" + ");";
jaimin
  • 563
  • 8
  • 25