我正在努力创建一款Android应用。我是Android和Java应用程序开发的初学者,同时我有很好的Web开发经验。
我正在尝试创建一个数据库,然后在SQLite中创建我的第一个表。它正在创建数据库而不是数据库表。
我创建了Contaract类。
package com.haafiz.project;
import android.provider.BaseColumns;
public final class ProjectContaract {
public ProjectContaract(){}
public static abstract class Site implements BaseColumns{
public static final String TABLE_NAME = "site";
public static final String COLUMN_NAME_SITE_NAME = "site_name";
public static final String COLUMN_NAME_LOGIN = "login_name";
public static final String COLUMN_NAME_HOST = "host";
public static final String COLUMN_NAME_PASSWORD = "password";
}
}
然后是DB助手类。
package com.haafiz.project;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + ProjectContaract.Site.TABLE_NAME + " (" +
projectContaract.Site._ID + " INTEGER PRIMARY KEY," +
projectContaract.Site.COLUMN_NAME_SITE_NAME + TEXT_TYPE + COMMA_SEP +
projectContaract.Site.COLUMN_NAME_LOGIN + TEXT_TYPE + COMMA_SEP +
projectContaract.Site.COLUMN_NAME_HOST + TEXT_TYPE + COMMA_SEP +
projectContaract.Site.COLUMN_NAME_PASSWORD + TEXT_TYPE + " )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + ProjectContaract.Site.TABLE_NAME;
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Project";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
这里我使用DB Helper的主要活动如下:
package com.haafiz.project;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
projectDBHelper dbHelper = new projectDBHelper(getApplicationContext());
// Gets the data repository in read mode
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Projection that specifies which columns from the database
String[] projection = {
projectContaract.Site._ID,
projectContaract.Site.COLUMN_NAME_SITE_NAME,
projectContaract.Site.COLUMN_NAME_LOGIN,
projectContaract.Site.COLUMN_NAME_HOST,
projectContaract.Site.COLUMN_NAME_PASSWORD
};
// How you want the results sorted in the resulting Cursor
String sortOrder =
projectContaract.Site.COLUMN_NAME_SITE_NAME + " DESC";
Cursor c = db.query(
projectContaract.Site.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
正在创建数据库,但表不是,我检查adb shell附加模拟器。我究竟做错了什么?该表尚未创建。
重要的是我在日志中没有收到与此相关的错误。
但是在下面的“没有过滤器”的logcat中,以前是“仅显示选定的应用程序”而过滤搜索框中的“sql”,它会在log cat中显示以下消息:
08-04 00:41:35.494 403-474/android.process.media V/MediaScanner﹕ pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@41302da8
08-04 00:41:35.494 403-474/android.process.media V/MediaScanner﹕ /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@41302da8
08-04 00:54:02.685 623-632/com.haafiz.project E/SQLiteDatabase﹕ close() was never explicitly called on database '/data/data/com.haafiz.project/databases/project'
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
at com.haafiz.project.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:4466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
08-04 00:54:02.766 623-632/com.haafiz.project E/System﹕ java.lang.IllegalStateException: Don't have database lock!
at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
at android.util.LruCache.trimToSize(LruCache.java:197)
at android.util.LruCache.evictAll(LruCache.java:285)
at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
at java.lang.Thread.run(Thread.java:856)
除前两行外,log cat输出中的其他行,所有其他行以红色突出显示。
答案 0 :(得分:1)
好像数据库没有正确关闭。有时,当您经常重新安装应用程序的构建时,这可能发生在开发期间。
卸载应用并重新安装。或者打开设置&gt;应用&gt; [您的应用]并点按“清除数据”按钮。其中任何一个都将删除现有的数据库文件,因此下次打开它的尝试将创建一个新文件。
答案 1 :(得分:0)
SQLLite在许多模式下运行,其中之一是所有内容都存储在内存中而不会刷新到磁盘,除非满足某些条件(如高内存使用率或数据库被关闭)。您没有关闭数据库,这就是您无法在sqlite文件中看到表的原因。
要使您的示例正常工作,请将dbHelper存储为类属性,在活动初始化时为其分配新对象,然后在关闭/销毁活动之前将其关闭。这应该可以解决你的问题。
答案 2 :(得分:0)
您在create table SQL语句末尾缺少分号。此外,您的主键应该是自动增量。
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + ProjectContaract.Site.TABLE_NAME + " (" +
projectContaract.Site._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
projectContaract.Site.COLUMN_NAME_SITE_NAME + TEXT_TYPE + COMMA_SEP +
projectContaract.Site.COLUMN_NAME_LOGIN + TEXT_TYPE + COMMA_SEP +
projectContaract.Site.COLUMN_NAME_HOST + TEXT_TYPE + COMMA_SEP +
projectContaract.Site.COLUMN_NAME_PASSWORD + TEXT_TYPE + " );";