【问题标题】:creating sqlite database on device using AndroidStudio使用 Android Studio 在设备上创建 sqlite 数据库
【发布时间】:2015-11-11 09:59:55
【问题描述】:

我目前正在做我的学校作业。在调试我的应用程序时,我目前在创建 SQLITE DB 时遇到了一些问题。

logcat:

E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/data/data/com.asus.microfilm/databases/Sugar.db) -
E/SQLiteDatabase﹕ Failed to open database '/data/data/com.asus.microfilm/databases/Sugar.db'.

我想知道为什么它会打开另一个包数据库。我在网上搜索了一些示例,但找不到任何解决方案,这就是我决定在这里发布的原因。

另外,我已经用过adb-shell了,我的包里什么也没找到。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

tab1.java

DatabaseHelper myDb;
myDb = new DatabaseHelper(getActivity());

我的 tab1.java 是一个片段。

【问题讨论】:

  • 更新到context.getExternalFilesDir 我想把它保存到我的 SD 卡但是当我在 DDMS 中打开我的package>files 时仍然没有
  • 你想做什么?
  • 你想把数据库保存在哪里?
  • 我正在尝试将我的 *.db 文件保存到我的 SD 卡中。由于我的手机没有root。我无法访问内部存储中的data>data 文件夹
  • 谁说你不能访问数据>数据文件夹?

标签: android sqlite android-studio


【解决方案1】:

例如我的解决方案

public class NotesDB extends SQLiteOpenHelper {


    private static final String DB_NAME = "NotesFile";
    private static final String LOG_TAG = "myLogs";
    private static final String TABLE_NAME = "Notes";
    private static final String COLUMN_KEY = "key";
    private static final String COLUMN_NOTE = "note";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_QUESTION = "question";
    private String table = "";

    public NotesDB(Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(LOG_TAG, "--- onCreate database ---");
        db.execSQL("create table "+TABLE_NAME+" ("
                + "id integer primary key autoincrement,"
                + COLUMN_NAME+" text,"
                + COLUMN_KEY+" integer,"
                + COLUMN_QUESTION+" text,"
                + COLUMN_NOTE+" text" + ");");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

用法

SQLiteDatabase db = new NotesDB(getAppContext()).getWritableDatabase();
        c=db.query("Notes", null, selection, selectionArgs , null, null, null );................

【讨论】:

  • 嗨@latsenko Anton,我试过了。不能工作!当我启动应用程序时,节目立即停止。 :(
【解决方案2】:

试试这个:

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

【讨论】:

  • 嗨@walkmn,我试过这个。它不起作用,我不知道为什么。当我检查我的 logcat 时,它显示 /data/data/com.asus.microfilm/databases/Sugar.db 这与我的包完全不同。
【解决方案3】:

试试这个,如果它在那里创建文件,请告诉我

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, context.getExternalFilesDir("MyDatabase") + "/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

或者如果你想使用内存,你也可以试试这个,因为它更安全

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, context.getFilesDir() + "/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

更新 1:尝试使用这个

 public DatabaseHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + FILE_DIR
            + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}

【讨论】:

  • 不,使用 DDMS 检查。 :(
  • 您在哪个目录中查找?
  • storage>MicroSD>Android>com.user.myassignment2v04>Files,我说的对吗?
  • no 您在错误的目录中搜索,可能是数据库是由我建议的解决方案创建的,但是当您在错误的路径中搜索时您找不到它,,
  • 我已经添加了日志,以便您可以看到确切的路径,并在评论中与我分享该路径
【解决方案4】:

SQLiteOpenHelper 有两个构造函数:

SQLiteOpenHelper(Context context, String name,    SQLiteDatabase.CursorFactory factory, int version)


SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)

你必须实现其中之一,并在 onCreate 方法中创建表。

【讨论】:

    猜你喜欢
    • 2015-03-29
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多