所以我正在开发一个应用程序,当用户点击按钮时,该应用程序会从数据库中拉出一个随机行。我正在使用
https://github.com/jgilfelt/android-sqlite-asset-helper
作为数据库助手。我用
创建了一个数据库我已按照文档中的每条指令进行操作。当我尝试运行应用程序时,当我按下按钮时它会崩溃。按下按钮会显示一个应该显示数据的新活动。当我拉出文件的属性时,它被列为数据库。以下是logcat中显示错误的选项:
03-10 18:52:07.476 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke W/SQLiteAssetHelper﹕ copying database from assets...
03-10 18:52:07.488 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke W/SQLiteAssetHelper﹕ database copy complete
03-10 18:52:07.544 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke E/SQLiteLog﹕ (26) file is encrypted or is not a database
03-10 18:52:07.556 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke E/DefaultDatabaseErrorHandler﹕ Corruption reported by sqlite on database: /data/data/tomschinler.thegreatman_kiniskamikazekaraoke/databases/mankiniDB.db
03-10 18:52:07.556 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke E/DefaultDatabaseErrorHandler﹕ deleting the database file: /data/data/tomschinler.thegreatman_kiniskamikazekaraoke/databases/mankiniDB.db
03-10 18:52:07.568 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke E/SQLiteLog﹕ (14) cannot open file at line 30191 of [00bb9c9ce4]
03-10 18:52:07.568 10812-10812/tomschinler.thegreatman_kiniskamikazekaraoke E/SQLiteLog﹕ (14) os_unix.c:30191: (2) open(/data/data/tomschinler.thegreatman_kiniskamikazekaraoke/databases/mankiniDB.db) -
它成功复制数据库,然后无法打开它。
这是数据库助手类:
package tomschinler.thegreatman_kiniskamikazekaraoke;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
/**
* Created by TomandGina on 3/9/2015.
*/
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "mankiniDB.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Cursor kamikazeSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList ORDER BY random () limit 1", null);
return cursor;
}
}
这是调用数据库的类:
package tomschinler.thegreatman_kiniskamikazekaraoke;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.IOException;
public class SongActivity extends ActionBarActivity {
private MyDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
//when activity launches query database using user params
TextView songTitle = (TextView)findViewById(R.id.songTitle);
TextView songArtist = (TextView)findViewById(R.id.songArtist);
TextView songYear = (TextView)findViewById(R.id.songYear);
db= new MyDatabase(this);
Cursor cursor = db.kamikazeSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle.setText(cursor.getString(2));
songArtist.setText(cursor.getString(3));
songYear.setText(cursor.getInt(4));
}
cursor.close();
}
编辑:我正在从应用程序添加db文件:
BEGIN TRANSACTION;
CREATE TABLE songList (
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`Title` TEXT,
`Artist` TEXT,
`Decade` INTEGER,
`Year` INTEGER,
`Genre` TEXT
);
INSERT INTO songList VALUES (1,'Great Balls of Fire','Jerry Lee Lewis',50,1957,'Rock');
INSERT INTO songList VALUES (2,'Ring of Fire','Johnny Cash',60,1963,'Country');
INSERT INTO songList VALUES (3,'Shaft','Shaft',70,1971,'Blues');
INSERT INTO songList VALUES (4,'Hotel California','Eagles',70,1973,'Rock');
INSERT INTO songList VALUES (5,'Take On Me','AHA',80,1985,'Pop');
INSERT INTO songList VALUES (6,'Don''t Stop Believing','Journey',80,1981,'Rock');
INSERT INTO songList VALUES (7,'Friends in Low Places','Garth Brooks',90,1991,'Country');
INSERT INTO songList VALUES (8,'Baby Got Back','Sir Mix-A-Lot',90,1992,'Hip Hop');
INSERT INTO songList VALUES (9,'Hey Ya','OutKast',0,2000,'Hip Hop');
INSERT INTO songList VALUES (10,'I Kissed a Girl','Katy Perry',0,2007,'Pop');
INSERT INTO songList VALUES (11,'Just the way you are','Bruno Mars',10,2010,'Pop');
INSERT INTO songList VALUES (12,'Super Bass','Nicki Minaj',10,2010,'Hip Hop');
CREATE TABLE android_metadata ("locale" TEXT DEFAULT 'en_US');
INSERT INTO android_metadata VALUES ('en_US');
COMMIT;
我已经看过Stack Overflow并对Google非常熟悉,但我无法弄清楚这一点。请帮忙。