表未创建sqlite android [英] Table not getting created sqlite android

查看:65
本文介绍了表未创建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代码

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

推荐答案

我还有另一个具有相同数据库名称的表groupInfo,所以出现了问题?

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

这是一个问题.这是发生了什么:

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

  • 访问具有相同数据库文件的第一个sqlite打开帮助器.如果数据库文件不存在,则会调用onCreate()回调,以便您可以设置数据库文件.

  • 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.

具有相同数据库文件的另一个sqlite打开帮助器被访问.具有给定名称的数据库文件已经存在并且具有正确的版本,因此不会调用onCreate()onUpgrade().而是仅打开文件.

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.

解决方案:每个数据库文件仅使用一个sqlite打开帮助器.将两个表的创建语句放在相同的帮助器onCreate()方法中.

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.

请参阅链接的问题何时运行SQLiteOpenHelper onCreate()/onUpgrade()?了解有关sqlite开放式帮助程序生命周期回调的更多信息.

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

这篇关于表未创建sqlite android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆