我尝试从 Sqlite浏览器中读取数据库中的数据。一切都好,但我看不到结果。我将mydb.db
复制到我在项目根目录中创建的assets文件夹中。
我的问题在哪里?
感谢。
public class DatabasesActivity extends Activity {
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_databases);
DBAdapter db = new DBAdapter(this);
try {
String destPath = "/data/data/"+getPackageName()+"/databases";
File f=new File(destPath);
if (!f.exists()) {
f.mkdirs();
f.createNewFile();
CopyDB(getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath+"/MyDB"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
db.open();
Cursor c=db.getAllContacts();//.getAllContacts();
if(c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
db.close();
}
public void CopyDB(InputStream inputStream,
OutputStream outputStream) throws IOException {
byte[] buffer=new byte[1024];
int lenght;
while ((lenght=inputStream.read(buffer))>0) {
outputStream.write(buffer,0,lenght);
Toast.makeText(this,"copy", Toast.LENGTH_LONG).show();
}
inputStream.close();
outputStream.close();
}
public void DisplayContact(Cursor c) {
Toast.makeText(this,"Name: "+c.getString(1)+"\n"+
"Date: "+c.getString(2)+"\n"+
"Email: ",
Toast.LENGTH_LONG).show();
}
}
这是我的 DBAdapter :
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_EMAIL};
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_TASK = 1;
public static final int COL_DATE = 2;
// DataBase info:
public static final String DATABASE_NAME = "MyDB";
public static final String DATABASE_TABLE = "contacts";
public static final int DATABASE_VERSION = 1; // The version number must be incremented each time a change to DB structure occurs.
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_EMAIL + " TEXT"
+ ");";
private final Context context;
private DataBaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DataBaseHelper(context);
}
private static class DataBaseHelper extends SQLiteOpenHelper {
DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE_SQL);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
}
}
// Open the database connection.
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
DBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertContact(String name, String email) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EMAIL, email);
return db.insert(DATABASE_TABLE,null,initialValues);
// Insert the data into the database.
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteContact(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) > 0;
}
public Cursor getAllContacts() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
/*public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}*/
// Return all data in the database.
// Get a specific row (by rowId)
public Cursor getContacts(long rowId) throws SQLException {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String name, String email) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, name);
newValues.put(KEY_EMAIL, email);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
}
这是我的 Sqlite浏览器屏幕截图:
答案 0 :(得分:0)
作为@CL。说,你应该使用SQLiteAssetHelper。
如果您使用基于Gradle的IDE,例如Android Studio
,只需在 app build.gradle 文件中添加以下依赖项:
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
如果您使用Eclipse
,请下载最新的库JAR并将其放入项目的libs文件夹中。从this JAR link下载。
添加库后,通过在 DatabaseHelper 中更改以下代码来扩展 DatabaseHelper SQLiteAssetHelper :
private static class DataBaseHelper extends SQLiteOpenHelper
为:
private static class DataBaseHelper extends SQLiteAssetHelper
然后将您的数据库添加到资产文件夹,记得首先压缩或gzip,它位于数据库文件夹中,如:
assets/databases/mydb.db.zip
了解详情