我有一个应用需要访问静态数据库并根据用户输入运行查询。我正在使用
jgilfelt / Android的源码资产辅助
作为我的数据库助手类。我的主要活动有2个微调器,用户可以使用它来缩小搜索范围。微调器分别返回名为mGenre和mDecade的字符串变量。在活动的底部是一个继续按钮。当按下按钮时,意图触发以显示随机显示歌曲标题的新活动。使用putExtra()将变量传递给新活动vi。新活动接收变量并在if / else if语句中使用它们来决定从DB Helper运行哪个查询。
在DB帮助程序类中,我创建了一个新类的实例,并使用它来设置mGenre和mDecade变量,以便使用rawQuery()的String [] args部分中的变量运行正确的查询
这是当用户点击主活动上的按钮时打开的songActivity类:
public class SongActivity extends ActionBarActivity {
private MyDatabase db;
private static final String TAG = "We got ";
String mGenre;
String mDecade;
public String getGenre() {
return mGenre;
}
public String getDecade() {
return mDecade;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
//when activity launches query database using user params
mGenre = getIntent().getExtras().getString("Genre");
mDecade = getIntent().getExtras().getString("Decade");
Log.v(TAG, mGenre);
Log.v(TAG, mDecade);
String songTitle = "";
String songArtist = "";
String songYear = "";
TextView mSongTitle = (TextView)findViewById(R.id.songTitle);
TextView mSongArtist = (TextView)findViewById(R.id.songArtist);
TextView mSongYear = (TextView)findViewById(R.id.songYear);
db= new MyDatabase(this);
if (mGenre.equals("*") && mDecade.equals("*")) {
Cursor cursor = db.kamikazeSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
else if (!mGenre.equals("*") && mDecade.equals("*")) {
Cursor cursor = db.genreSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
// Log.i("cursor", cursor.getString(1));
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
else if (mGenre.equals("*") && !mDecade.equals("*")) {
Cursor cursor = db.decadeSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
else if (!mGenre.equals("*") && !mDecade.equals("*")) {
Cursor cursor = db.bothSelected();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
cursor.moveToNext();
}
cursor.close();
db.close();
}
mSongTitle.setText(songTitle);
mSongArtist.setText(songArtist);
mSongYear.setText(songYear);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是数据库助手类:
public class MyDatabase extends SQLiteAssetHelper {
private static final String TAG = "Variable for query is ";
private static final String DATABASE_NAME = "mankiniDB.sqlite";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
SongActivity mSongActivity = new SongActivity();
String mGenre = mSongActivity.getGenre();
String mDecade = mSongActivity.getDecade();
public Cursor kamikazeSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList ORDER BY random() limit 1", null);
return cursor;
}
public Cursor genreSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Genre = ? ORDER BY random() limit 1", new String[] {mGenre});
return cursor;
}
public Cursor decadeSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Decade = ? ORDER BY random() limit 1", new String[] {mDecade});
return cursor;
}
public Cursor bothSelected() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Decade = ? AND Genre = ? ORDER BY random() limit 1", new String[] {mDecade, mGenre});
return cursor;
}
}
这是LogCat错误堆栈跟踪:
03-19 21:35:54.256 1751-1751/tomschinler.thegreatman_kiniskamikazekaraoke I/SQLiteAssetHelper﹕ successfully opened database mankiniDB.sqlite
03-19 21:35:54.260 1751-1751/tomschinler.thegreatman_kiniskamikazekaraoke D/AndroidRuntime﹕ Shutting down VM
03-19 21:35:54.264 1751-1751/tomschinler.thegreatman_kiniskamikazekaraoke W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4b61648)
03-19 21:35:54.276 1751-1751/tomschinler.thegreatman_kiniskamikazekaraoke E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tomschinler.thegreatman_kiniskamikazekaraoke/tomschinler.thegreatman_kiniskamikazekaraoke.SongActivity}: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at tomschinler.thegreatman_kiniskamikazekaraoke.MyDatabase.genreSelected(MyDatabase.java:50)
at tomschinler.thegreatman_kiniskamikazekaraoke.SongActivity.onCreate(SongActivity.java:82)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
问题是,无论我做什么,或者我在哪里设置变量,数据库助手类都会重新生成并且空变量会导致出现null参数。我需要用户的输入完成查询。但它始终是空的。请帮我看看我错过了什么。
答案 0 :(得分:0)
您不应该自己创建Activity
实例(new SongActivity()
),这不起作用。您需要与现有的Activity
实例进行通信,例如将其传递给数据库处理程序,但不建议这样做。最好的方法是仅传递所需的数据,因此您的活动和数据库处理程序是分离的。
例如:
public Cursor genreSelected(final String genre) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor =db.rawQuery("SELECT * FROM songList WHERE Genre = ? ORDER BY random() limit 1", new String[] {genre});
return cursor;
}
考虑将查询字符串移动到您的类上private static final
,这样可以从多个部分访问它们和/或更轻松地修改它们。
您对Cursor
的处理也很奇怪。这是正确的方法:
try {
while (cursor.moveToNext()) {
songTitle = cursor.getString(1);
songArtist = cursor.getString(2);
songYear = cursor.getString(4);
Log.i(TAG, String.valueOf(songTitle));
}
} finally {
cursor.close()
}
答案 1 :(得分:0)
public class DBHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "UserInfo.db";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE =
"CREATE TABLE " + UserProfile.Users.TABLE_NAME + " (" +
UserProfile.Users._ID + " INTEGER PRIMARY KEY," +
UserProfile.Users.COLUMN_USERNAME + " TEXT," +
UserProfile.Users.COLUMN_DOB + " TEXT," +
UserProfile.Users.COLUMN_GENDER + " TEXT," +
UserProfile.Users.COLUMN_PASSWORD + " TEXT )";
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long addInfo(String username, String password){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(UserProfile.Users.COLUMN_USERNAME, username);
contentValues.put(UserProfile.Users.COLUMN_PASSWORD, password);
long rowId = sqLiteDatabase.insert(UserProfile.Users.TABLE_NAME, null, contentValues);
return rowId;
}
public int updateInfo(String userId, String userName, String password, String dob, String gender){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(UserProfile.Users.COLUMN_USERNAME, userName);
values.put(UserProfile.Users.COLUMN_PASSWORD, password);
values.put(UserProfile.Users.COLUMN_GENDER, gender);
values.put(UserProfile.Users.COLUMN_DOB, dob);
String selection = UserProfile.Users._ID + " = ?";
String args[] = {userId};
int count = sqLiteDatabase.update(UserProfile.Users.TABLE_NAME, values, selection, args);
return count;
}
public ArrayList readAllInfo(){
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
String[] projection = {
UserProfile.Users._ID,
UserProfile.Users.COLUMN_USERNAME,
UserProfile.Users.COLUMN_DOB,
UserProfile.Users.COLUMN_GENDER,
UserProfile.Users.COLUMN_PASSWORD
};
String sortOrder = UserProfile.Users._ID + " DESC";
Cursor cursor = sqLiteDatabase.query(
UserProfile.Users.TABLE_NAME,
projection,
null,
null,
null,
null,
sortOrder
);
ArrayList<User> list = new ArrayList<>();
if (cursor.getCount() > 0){
while(cursor.moveToNext()){
User newUser = new User();
int id = cursor.getInt(cursor.getColumnIndexOrThrow(UserProfile.Users._ID));
String user = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_USERNAME));
String date = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_DOB));
String gen = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_GENDER));
String pass = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_PASSWORD));
newUser.setUserId(id+"");
newUser.setUserName(user);
newUser.setDateOfBirth(date);
newUser.setGender(gen);
newUser.setPassword(pass);
list.add(newUser);
}
}
return list;
}
public ArrayList readAllInfo(String userId, String userName){
String selection;
String[] args = {""};
if(userId == null){
selection = UserProfile.Users.COLUMN_USERNAME + " LIKE ?";
args[0] = userName;
}
else
{
selection = UserProfile.Users._ID + " = ?";
args[0] = userId;
}
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
String[] projection = {
UserProfile.Users._ID,
UserProfile.Users.COLUMN_USERNAME,
UserProfile.Users.COLUMN_DOB,
UserProfile.Users.COLUMN_GENDER,
UserProfile.Users.COLUMN_PASSWORD
};
String sortOrder = UserProfile.Users._ID + " DESC";
Cursor cursor = sqLiteDatabase.query(
UserProfile.Users.TABLE_NAME,
projection,
selection,
args,
null,
null,
sortOrder
);
ArrayList<User> list = new ArrayList<>();
if (cursor.getCount() > 0){
while(cursor.moveToNext()){
User newUser = new User();
int id = cursor.getInt(cursor.getColumnIndexOrThrow(UserProfile.Users._ID));
String user = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_USERNAME));
String date = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_DOB));
String gen = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_GENDER));
String pass = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_PASSWORD));
newUser.setUserId(id+"");
newUser.setUserName(user);
newUser.setDateOfBirth(date);
newUser.setGender(gen);
newUser.setPassword(pass);
list.add(newUser);
}
}
return list;
}
public int deleteInfo(String username){
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
String selection = UserProfile.Users._ID + " = ?";
String[] args = {username};
int deletedRows = sqLiteDatabase.delete(UserProfile.Users.TABLE_NAME, selection, args);
return deletedRows;
}
}