我正在使用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>
和logcat:
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
答案 0 :(得分:1)
试试这个
ourHelper = new DbHelper(ourcontext);
ourDatabase = ourHelper.getWritableDatabase();
ourHelper .onCreate(ourDatabase );
答案 1 :(得分: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";
请尝试。
答案 2 :(得分:0)
在你的日食中转到Window&gt;显示视图&gt;文件管理器 并查看数据&gt;数据&gt; yourPackageName是否存在数据库,如果没有,则必须在其中导入数据库文件。