TransWikia.com

Table not getting created sqlite android

Stack Overflow Asked by user3844417 on December 9, 2021

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???

4 Answers

If you're are viewing the database in SQLite viewer, you have to download all of the three files associated with .db file.

For instance, if your database name is customer.db, download

  1. customer.db,
  2. customer.db-shm and
  3. customer.db-wal

Everthing works but when viewing in the sqlite viewer, you don't see any table if you only download customer.db file. Down right in the Device File Explorer

To access the .db file, go to View->Tool Windows->Device File Explorer->data->data> -->databases

Answered by Nima on December 9, 2021

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.

Answered by laalto on December 9, 2021

Your table was not created so the system cannot insert data.

android.database.sqlite.SQLiteException: no such table: contactsDetails (code 1):

I suggest you to check your SQL Request then use this snippet of code:

private static final String DATABASE_NAME = "YOURDATABASE.db";


   public DBHelper(Context context, String tableName) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mTableName = tableName;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        Log.d(TAG, "Create application database");
        database.execSQL(createTable(mTableName));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + mTableName);
        onCreate(db);
    }

    private String createTable(String tableName) {
        return createTable;
    }

Answered by Renaud Mathieu on December 9, 2021

change private static final String dbName = "HSsuraksha"; to private static final String dbName = "HSsuraksha.db"; you created database but without extension so add extension .db to it like HSsuraksha.db

Answered by jaimin on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP