简体   繁体   English

Android-如何从内置电话访问sqlite数据库?

[英]Android - How to access sqlite database from internal memory phone?

I've created an app using Eclipse + ADT that reads and write into a sqlite db, it works fine on Android terminal emulator, but, running from the phone itself, it does not populate the Spinners from db, which means it does not locate db in internal memory, I already checked with adb shell if the db is on the phone and it's there, so I guess I need to give root permissions to the app to allow it to use the db, how can I do this? 我使用Eclipse + ADT创建了一个可读写sqlite db的应用程序,它在Android终端仿真器上运行良好,但是从手机本身运行,它不会从db中填充Spinners,这意味着它无法定位内部存储器中的db,我已经使用adb shell检查了该db是否在电话上并且它已经存在,所以我想我需要为该应用授予root权限以允许它使用该db,我该怎么做? my phone is a Motorola Defy and it's rooted, thanks in advanced, have a nice day. 我的手机是Motorola Defy,它扎根,谢谢您,祝您愉快。

EDIT: This is my class to create the table and adds the methods onCreate and onUpdate: 编辑:这是我的类来创建表并添加方法onCreate和onUpdate:

public class passwordSQLiteHelper extends SQLiteOpenHelper{

String SqlCreate = "CREATE TABLE password(id INTEGER, name TEXT, password TEXT)";

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

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(SqlCreate);

}

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

}

}

And I'm calling it from the main class when the activity starts: 活动开始时,我从主类中调用它:

passwordSQLiteHelper passUserDb = new passwordSQLiteHelper(this, "/data/data/com.project.otto/databases/password", null, 1);

On the terminal works great, but not on the phone where seems like it can't find the db, I checked with adb and it's there, please help me out, thanks! 在终端上运行良好,但在似乎无法找到数据库的电话上却没有,我检查了adb并找到了它,请帮帮我,谢谢!

First, I wouldn't specify the full path to the database in the constructor. 首先,我不会在构造函数中指定数据库的完整路径。 Just the filename should be fine (since Android will automatically create it in your applications private data area). 只是文件名应该没问题(因为Android会在您的应用程序私有数据区域中自动创建文件名)。

public PasswordDatabase(Context context {
    super(context, "password.db", null, 1);
}

And create it via: 并通过以下方式创建它:

PasswordDatabase db = new PasswordDatabase(this);

If you look at the docs for SQLiteOpenHelper , it states that the database will not be created until either getReadableDatabase() or getWriteableDatabase() are called. 如果您查看SQLiteOpenHelper的文档,它将指出直到getReadableDatabase()getWriteableDatabase()才会创建数据库。 So, I'd try calling one of those methods and then check that the database was created. 因此,我将尝试调用这些方法之一,然后检查是否已创建数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM