gpt4 book ai didi

android - 如何从 ANDROID 中的 sqlite 数据库中获取列数据?

转载 作者:行者123 更新时间:2023-11-29 22:21:43 25 4
gpt4 key购买 nike

我是第一次在 Android 中使用 sqlite 数据库。我已经在 Sqlite 中创建了我的数据库并将其复制到 androids "data/data/App package/databases"。我能够复制和打开数据库。我在数据库中有四个表,我想要 “State_Master(Table)”中的“State_Name(Column)”

但我无法从表中获取任何数据(州名)。

我在这里发布我的代码:

//DataBase Helper

public class DatabaseHelper extends SQLiteOpenHelper {

private String[] stateArray;
private Context context;
private String DB_PATH;
private static String DB_NAME = "myDatabase.sqlite";
public static SQLiteDatabase myDatabase;
public static final String STATE_NAME = "State_Name";

public LoveGodDB(Context context) throws Exception {
super(context, DB_NAME, null, 1);
this.context = context;
DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
boolean dbexist = checkdatabase();
if (dbexist) {
opendatabase();
} else {
createdatabase();
}

}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(STATE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void createdatabase() throws Exception {
boolean dbexist = checkdatabase();
if (dbexist) {
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() throws Exception {
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch (SQLiteException sle) {
throw sle;
}
return checkdb;
}
private void copydatabase() throws IOException {

InputStream myinput = context.getApplicationContext().getAssets().open(DB_NAME);
OutputStream myoutput = new FileOutputStream("/data/data/"+ context.getApplicationContext().getPackageName() +"/databases/myDataBase.sqlite");
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer)) > 0) {
myoutput.write(buffer, 0, length);
}

myoutput.flush();
myoutput.close();
myinput.close();
}
public void opendatabase() throws SQLException {
String mypath = DB_PATH + DB_NAME;
LGDataBase = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READWRITE);

}
public synchronized void close() {
if (LGDataBase != null) {
LGDataBase.close();
}
super.close();
}
public Cursor fetchAllTopics() {

return LGDataBase.query("State_Master", new String[] {STATE_NAME},
null,
null,
null,
null,
null);
};


}

和我想在字符串数组中存储数据的 Activity :

public class MyStateGrid extends Activity{

private Button back;
private String[] stateNameArray ;
private GridView gridview = null;
private DatabaseHelper lgDB;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.errandboy_state);

back = (Button) findViewById(R.id.back);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

//stateNameArray = getResources().getStringArray(R.array.stateName);
try {
lgDB = new LoveGodDB(this);
lgDB.opendatabase();
Cursor c = lgDB.fetchAllTopics();
startManagingCursor(c);
stateNameArray = new String[]LoveGodDB.STATE_NAME};
} catch (IOException e) {

} catch (Exception e) {

}finally{
if(lgDB != null){
lgDB.close();
}
}

gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent stamp = new Intent(getParent(), xxxx.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("Stamp", stamp);

}
});

OnTouchListener backListener = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_UP){
finish();
}
return false;
}
};

back.setOnTouchListener(backListener);
}
public class ImageAdapter extends BaseAdapter {
@SuppressWarnings("unused")
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return stateImageArray.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.galchild, null);
ImageView stampImage;
TextView stampName;
stampImage = (ImageView) v.findViewById(R.id.ImageView01);
stampName = (TextView) v.findViewById(R.id.stampName);
stampImage.setScaleType(ImageView.ScaleType.FIT_XY);
stampImage.setImageResource(R.drawable.stamps);
stampName.setText(stateNameArray[position]);
return v;
}
}

有什么建议吗?

谢谢...

最佳答案

我不明白这一行:

stateNameArray = new String[]LoveGodDB.STATE_NAME

尝试将其更改为:

stateNameArray = new String[c.count()];

然后需要将Cursor中的状态名称放入String数组中:

int i = 0;
c.moveToFirst();
while (c.isAfterLast() == false) {
statNameArray[i] = c.getString(0);
i++;
c.moveToNext();
}

答案改编自:Iterate through rows from Sqlite-query

关于android - 如何从 ANDROID 中的 sqlite 数据库中获取列数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7006754/

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