Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table not getting created sqlite android

I am making an application in which i want to save user contacts details.But whenever i try to insert or select some values i get an error.

DataBasae code

public class ContactsDatabase extends SQLiteOpenHelper {

    private static final int dbVersion = 1;
    private static final String dbName = "HSsuraksha";
    private static final String tableName = "contactsDetails";
    private static final String contactId = "contactId";
    private static final String groupId = "groupId";
    private static final String groupGroupId = "groupId";
    private static final String groupTable = "groupDetails";
    private static final String contactName = "contactName";
    private static final String contactNumber = "contactNumber";
    private static final String createTable = "Create Table " + tableName + "(" + contactId + " Integer Primary Key AutoIncrement," + groupId + " Text," + contactName + " Text," + contactNumber + " Text" + ");";

    public ContactsDatabase(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 insertContacts(ContactModel contactModel, String id, ArrayList<ContactModel> contactModelArrayList) {
        SQLiteDatabase database = getWritableDatabase();
        database.beginTransaction();
        ContentValues contentValues = new ContentValues();
        for (int i = 0; i < contactModelArrayList.size(); i++) {

            contentValues.put(contactName, contactModelArrayList.get(i).getContactName());
            contentValues.put(contactNumber, contactModelArrayList.get(i).getContactNumber());
            contentValues.put(groupId, id);
            if (contentValues != null) {
                Long value = database.insert(tableName, id, contentValues);
            }

        }


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

    public void selectContacts(String id) {
        String query = "Select * From " + tableName + " where " + groupId + "=?";
        SQLiteDatabase database = getWritableDatabase();
        Cursor cursor = database.rawQuery(query, new String[]{id});
        while (cursor.moveToNext()) {
            cursor.getString(cursor.getColumnIndexOrThrow(contactName));
        }
        cursor.close();
        database.close();

    }
}

LOGCAT

08-01 18:53:57.820      672-672/example.com.pocketdocs E/SQLiteLog﹕ (1) no such table: contactsDetails
08-01 18:53:57.830      672-672/example.com.pocketdocs E/SQLiteDatabase﹕ Error inserting groupId=2 contactNumber=+91 97 69 512114 contactName=Aaaaaaa
    android.database.sqlite.SQLiteException: no such table: contactsDetails (code 1): , while compiling: INSERT INTO contactsDetails(groupId,contactNumber,contactName) 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.ContactsDatabase.insertContacts(ContactsDatabase.java:54)
            at example.com.pocketdocs.Group.CreateNewGroup.onClick(CreateNewGroup.java:104)
            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)

I tried solving the error but no success.Whats the problem with my code???

like image 303
user3844417 Avatar asked Aug 01 '14 13:08

user3844417


1 Answers

i have another table groupInfo with same database name,so it that the problem??

It's a problem. Here's what happens:

  • The first sqlite open helper with the same database file is accessed. If the database file didn't exist, the onCreate() callback is invoked so that you can set up the database file.

  • The other sqlite open helper with the same database file is accessed. A database file with the given name already exists and is of the correct version, so no onCreate() or onUpgrade() gets invoked. Instead the file is just opened.

Solution: Use only one sqlite open helper per database file. Put both table's creation statements in the same helper onCreate() method.

Also uninstall your app so the old database file with just the other table is removed.

See the linked question When is SQLiteOpenHelper onCreate() / onUpgrade() run? to learn more about sqlite open helper lifecycle callbacks.

like image 79
laalto Avatar answered Sep 18 '22 23:09

laalto