2

我正在使用 SQLite 应用程序。我的应用程序打开了任何其他活动。

这是代码

    public class hotornot {
        public static final String KEY_ROWID = "_id";
        public static final String KEY_NAME = "persons_name";
        public static final String KEY_HOTNESS = "persons_hotness";
        private static final String DATABASE_NAME = "HotorNotdb";
        private static final String DATABASE_TABLE = "peopletable";
        private static final int DATABASE_VERSION = 1;
        private DbHelper ourHelper;
        private final Context ourcontext;
        private SQLiteDatabase ourDatabase;

        private static class DbHelper extends SQLiteOpenHelper {

            public DbHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                        + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);"

                );
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub
                db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
                onCreate(db);
            }

        }

        public hotornot(Context c) {
            ourcontext = c;
        }

        public hotornot open() throws SQLException {
            ourHelper = new DbHelper(ourcontext);
            ourDatabase = ourHelper.getWritableDatabase();
            return this;
        }

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

        public Long createentry(String name, String hotness) {
            ContentValues cv = new ContentValues();
            cv.put(KEY_NAME, name);
            cv.put(KEY_HOTNESS, hotness);
            return ourDatabase.insert(DATABASE_TABLE, null, cv);
        }

        public String getdata() {
            // TODO Auto-generated method stub
            String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
            Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                    null, null);
            String result = "";

            int iRow = c.getColumnIndex(KEY_ROWID);
            int iName = c.getColumnIndex(KEY_NAME);
            int iHotness = c.getColumnIndex(KEY_HOTNESS);

            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                result = result + c.getString(iRow) + " " + c.getString(iName)
                        + " " + c.getString(iHotness) + "\n";
            }

            return result;
        }
    }

这是清单文件:

<activity
    android:name=".sqliteexample"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>
<activity
    android:name=".sview"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="org.sqlite.SVIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

和日志猫:

07-25 12:38:12.349: ERROR/AndroidRuntime(323): Uncaught handler: thread main exiting due to uncaught exception
07-25 12:38:12.399: ERROR/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.sqlite/org.sqlite.sview}: android.database.sqlite.SQLiteException: no such table: peopletable: , while compiling: SELECT _id, persons_name, persons_hotness FROM peopletable
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.os.Looper.loop(Looper.java:123)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at java.lang.reflect.Method.invoke(Method.java:521)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at dalvik.system.NativeStart.main(Native Method)
07-25 12:38:12.399: ERROR/AndroidRuntime(323): Caused by: android.database.sqlite.SQLiteException: no such table: peopletable: , while compiling: SELECT _id, persons_name, persons_hotness FROM peopletable
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1108)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1066)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1143)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at org.sqlite.hotornot.getdata(hotornot.java:73)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at org.sqlite.sview.onCreate(sview.java:14)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-25 12:38:12.399: ERROR/AndroidRuntime(323):     ... 11 more
4

3 回答 3

1

试试这个

    ourHelper = new DbHelper(ourcontext);
    ourDatabase = ourHelper.getWritableDatabase();
    ourHelper .onCreate(ourDatabase );  
于 2012-07-25T06:55:35.493 回答
0

我认为 DbHelper 定义是错误的。它应该是这样的:

public DbHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }

并修改

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

请试试。

于 2012-07-25T07:14:46.653 回答
0

在您的 Eclipse 中,转到 Window > Show View > File Explorer 并在 data > data > yourPackageName 中检查数据库是否存在,如果不存在,则必须在其中导入数据库文件。

于 2012-07-25T07:42:06.427 回答