gpt4 book ai didi

Android 数据库查询无法正常工作

转载 作者:行者123 更新时间:2023-11-30 04:12:15 25 4
gpt4 key购买 nike

我做了一个数据库,里面有这样的信息 http://i170.photobucket.com/albums/u244/therealbbri06/database.jpg

我正在尝试使用它从数据库中获取组名

public String readNameFromPosition(int position) {
Cursor c;
c = myDataBase.query("hikingGroups", new String[] { "groupName" },
"position=\'" + position + "\'", null, null, null, null);
if (c.moveToFirst()) {
return c.getString(0);
} else {
return "" + c.getCount();
}
}

当 position = 0 时,groupName 被正确检索,但只要 position 等于任何其他有效数字,c.getCount() 就会返回“0”。有什么帮助吗?如果需要,我可以发布更多代码,只问。 readNameFromPosition() 也在我的 DataBaseHelper 类中。

更新:我的代码

package bry.Bicker.OjaiHiking;
import java.io.IOException;
import android.app.Activity;
import android.database.SQLException;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;

public class OjaiHikingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final DataBaseHelper theDb = new DataBaseHelper(getApplicationContext());
try {

theDb.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

theDb.openDataBase();

} catch (SQLException sqle) {

throw sqle;

}
final Gallery hikesGallery = (Gallery) findViewById(R.id.gallery1);
hikesGallery.setAdapter(new ImageAdapter(this));
hikesGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String groupName = "";
int position = hikesGallery.getSelectedItemPosition();
Toast.makeText(getApplicationContext(), "" + position,
Toast.LENGTH_SHORT).show();
groupName = theDb.readNameFromPosition(position);
TextView tV = (TextView) findViewById(R.id.groupName);
Typeface font = Typeface.createFromAsset(getAssets(),
"arialbd.ttf");
tV.setTypeface(font);
tV.setText(groupName);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}

});
hikesGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {

}
});
}
}

DataBaseHelper.java 代码

package bry.Bicker.OjaiHiking;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/bry.Bicker.OjaiHiking/databases/";

private static String DB_NAME = "Ojaiker.db";

private SQLiteDatabase myDataBase;

private final Context myContext;

public DataBaseHelper(Context context) {

super(context, DB_NAME, null, 1);
this.myContext = context;
}

public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();

if (dbExist) {
// do nothing - database already exist
} else {

this.getReadableDatabase();

try {

copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");

}
}

}

/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {

SQLiteDatabase checkDB = null;

try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);

} catch (SQLiteException e) {

// database does't exist yet.

}

if (checkDB != null) {

checkDB.close();

}

return checkDB != null ? true : false;
}

/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {

// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);

// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;

// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);

// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public void openDataBase() throws SQLException {

// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

if (myDataBase != null)
myDataBase.close();

super.close();

}

public String readNameFromPosition(int position) {
Cursor c;
c = myDataBase.query("hikingGroups", new String[] { "groupName" },
"position=\'" + position + "\'", null, null, null, null);
if (c.moveToFirst()) {
return c.getString(0);
} else {
return "" + c.getCount();
}
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

最佳答案

您确定您没有使用应用程序外部的值(例如在 SQLite 管理器中)更新数据库,然后假设新值将在新的运行中存在,然后运行该应用程序吗?您的数据库将不会使用新值重新构建,因为它存在于设备/模拟器上。

尝试重新安装该应用程序,然后再次安装它(一个大胆的猜测是您第一次启动数据库时只有一个值(位置 0))。

关于Android 数据库查询无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com