大家好 我想将歌曲名称从sd卡存储到数据库中。我使用以下代码,但它不存储歌曲名称。它存储错误的内容。 我的代码是:
final String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.SIZE };
Log.e("media",""+proj.length);
SQLiteDatabase db;
db = openOrCreateDatabase("TestData.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
final String CREATE_TABLE_COUNTRIES ="CREATE TABLE IF NOT EXISTS songs (bpm INTEGER,songname TEXT);";
db.execSQL(CREATE_TABLE_COUNTRIES);
Log.e("media","table created");
ContentValues values = new ContentValues();
for(int i=0;i<proj.length;i++){
values.put("bpm", 0);
values.put("songname", proj[i]);
db.insert("songs", null, values);
}
Log.e("media","insered");
Cursor cx = db.rawQuery("select * from songs" , null);
if (cx != null ) {
if (cx.moveToFirst()) {
do { int bpm=cx.getInt(0);
String songname =cx.getString(1);
Log.e("songs","bpm "+bpm+" songname "+songname);
}while (cx.moveToNext());
}
}
db.close();
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
Log.e("media1",""+musiccursor);
我的日志猫输出是:
05-02 12:28:02.716: ERROR/media(957): 4
05-02 12:28:03.156: ERROR/media(957): table created
05-02 12:28:03.986: ERROR/media(957): insered
05-02 12:28:03.986: ERROR/songs(957): bpm 0 songname _id
05-02 12:28:03.986: ERROR/songs(957): bpm 0 songname _data
05-02 12:28:03.986: ERROR/songs(957): bpm 0 songname _display_name
05-02 12:28:03.986: ERROR/songs(957): bpm 0 songname _size
05-02 12:28:04.026: ERROR/media1(957): android.content.ContentResolver$CursorWrapperInner@4357b868
请帮助我。
答案 0 :(得分:0)
您应该使用'SQLiteOpenHelper'来管理数据库表。然后,您可以使用它的函数'getReadableDatabase()'和'getWriteableDatabase()',它们返回一个'SQLiteDatabase'。这是执行此操作的常用方法。