Ich versuche, Daten aus der Datenbank zu lesen, die ich mit SQLite Browser. Alles ist in Ordnung, aber ich kann keine Ergebnisse sehen. Ich kopierte mydb.db
zu Assets Ordner, den ich im Stamm des Projekts macht.Holen Sie Daten von embeded sqlite in Paket
Wo ist mein Problem?
Danke.
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();
}
}
und das ist mein 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;
}
}
Hier ist mein SQLite Browser Screenshot:
Verwenden [SQLiteAssetHelper] (http://jgilfelt.github.io/android-sqlite-asset-helper/) statt. –
mein Compiler nicht SQLiteAssetHelper? – farshadto
ist diese Bibliothek? – farshadto