表没有创建sqlite android

时间:2014-08-01 13:25:10

标签: android android-sqlite

我正在制作一个我希望保存用户联系人详细信息的应用程序。但每当我尝试插入或选择某些值时,我都会收到错误。

DataBasae代码

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)

我尝试解决错误但没有成功。我的代码是什么问题???

4 个答案:

答案 0 :(得分:9)

  

我有另一个表groupInfo与相同的数据库名称,所以它的问题??

这是一个问题。这是发生的事情:

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

  • 访问具有相同数据库文件的其他sqlite打开助手。具有给定名称的数据库文件已存在且版本正确,因此不会调用onCreate()onUpgrade()。而是刚刚打开文件。

解决方案:每个数据库文件只使用一个sqlite open helper。将两个表的创建语句放在同一帮助器onCreate()方法中。

同时卸载您的应用,以便删除仅包含其他表的旧数据库文件。

请参阅链接问题When is SQLiteOpenHelper onCreate() / onUpgrade() run?以了解有关sqlite open helper生命周期回调的更多信息。

答案 1 :(得分:0)

private static final String dbName = "HSsuraksha";更改为private static final String dbName = "HSsuraksha.db"; 你创建了数据库,但没有扩展名,所以像HSsuraksha.db

一样添加扩展名.db

答案 2 :(得分:0)

您的表格未创建,因此系统无法插入数据。

  

android.database.sqlite.SQLiteException:没有这样的表:   contactsDetails(代码1):

我建议你查看你的SQL请求然后使用这段代码:

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;
    }

答案 3 :(得分:0)

如果要在SQLite查看器中查看数据库,则必须下载与.db文件关联的所有三个文件。

例如,如果您的数据库名称是customer.db,请下载

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

一切正常,但是在sqlite查看器中查看时,如果仅下载customer.db文件,则看不到任何表。 Down right in the Device File Explorer

要访问.db文件,请转到查看->工具Windows->设备文件资源管理器->数据->数据>->数据库