在sqlite中创建多个表

时间:2014-05-20 07:12:40

标签: android sqlite

我想在我的SQLite数据库中创建两个表。创建了一个表(exptable),但是第二个表(inctable)给了我一个没有这样的表的错误。

这是我的数据处理程序代码:

package com.example.pocketmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHandler {
    public static final String DATE= "date";
    public static final String EAMT ="eamt";
    public static final String EPAYEE= "epayee";
    public static final String ECATEGORY ="ecategory";
    public static final String TABLE_NAME= "exptable";

    public static final String IDATE= "idate";
    public static final String IAMT ="iamt";
    public static final String IPAYEE= "ipayee";
    public static final String ICATEGORY ="icategory";
    public static final String TABLE_NAME1= "inctable";

    public static final String DATA_BASE_NAME = "expdatabase";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_CREATE ="create table exptable (date text not null, eamt string not null, epayee text not null, ecategory text not null)";
    public static final String TABLE_CREATE1 ="create table inctable (idate text not null, iamt string not null, ipayee text not null, icategory text not null)"; 
    DataBaseHelper dbhelper;
    Context ctx;
    SQLiteDatabase db;

    public DataHandler(Context ctx)
    {
        this.ctx= ctx;
        dbhelper = new DataBaseHelper (ctx);
    }

    private static class DataBaseHelper extends SQLiteOpenHelper
    {
        public DataBaseHelper (Context ctx)
        {
            super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try{
            db.execSQL(TABLE_CREATE);
            db.execSQL(TABLE_CREATE1);
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS exptable");
            db.execSQL("DROP TABLE IF EXISTS inctable");
            onCreate(db);
        }
    }

    public DataHandler open()
    {
        db = dbhelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        dbhelper.close();
    }

    public long insertData(String date, String eamt, String epayee, String ecategory)
    {
        ContentValues content =  new ContentValues();
        content.put(DATE, date);
        content.put(EAMT, eamt);
        content.put(EPAYEE, epayee);
        content.put(ECATEGORY, ecategory);
        return db.insertOrThrow(TABLE_NAME, null, content);
    }

    public Cursor returnData()
    {
        return db.query(TABLE_NAME, new String[] {DATE, EAMT, EPAYEE, ECATEGORY},null,null, null, null, null);
    }

    public long insertData1(String idate, String iamt, String ipayee, String icategory)
    {
        ContentValues content1 =  new ContentValues();
        content1.put(IDATE, idate);
        content1.put(IAMT, iamt);
        content1.put(IPAYEE, ipayee);
        content1.put(ICATEGORY, icategory);
        return db.insertOrThrow(TABLE_NAME1, null, content1);
    }

    public Cursor returnData1()
    {
        return db.query(TABLE_NAME1, new String[] {IDATE, IAMT, IPAYEE, ICATEGORY},null,null, null, null, null);
    }
}

这是我的Log cat:

05-20 07:09:29.097: E/SQLiteLog(1545): (1) no such table: inctable
05-20 07:09:29.097: D/AndroidRuntime(1545): Shutting down VM
05-20 07:09:29.097: W/dalvikvm(1545): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-20 07:09:29.137: E/AndroidRuntime(1545): FATAL EXCEPTION: main
05-20 07:09:29.137: E/AndroidRuntime(1545): android.database.sqlite.SQLiteException: no such table: inctable (code 1): , while compiling: INSERT INTO inctable(iamt,idate,icategory,ipayee) VALUES (?,?,?,?)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at com.example.pocketmanager.DataHandler.insertData1(DataHandler.java:107)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at com.example.pocketmanager.Income$2.onClick(Income.java:72)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.view.View.performClick(View.java:4204)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.view.View$PerformClick.run(View.java:17355)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.os.Handler.handleCallback(Handler.java:725)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.os.Looper.loop(Looper.java:137)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at java.lang.reflect.Method.invoke(Method.java:511)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-20 07:09:29.137: E/AndroidRuntime(1545):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

首先,从帮助器try中删除catch - onCreate()。如果有问题,请不要压制它。

其次,卸载您的应用,以便删除旧的数据库文件。见When is SQLiteOpenHelper onCreate() / onUpgrade() run?