我是第一次使用Android中的sqlite数据库。我在Sqlite中创建了我的数据库并将其复制到了机器人"data/data/App package/databases"
。我能够复制和打开数据库。我在数据库中有四个表我想要"State_Name(Column)" from "State_Master(Table)"
。
但我无法从表中获取任何数据(州名)。
我在这里发布我的代码:
//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);
};
}
}
和我想要在String Array中存储数据的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;
}
}
}
有什么建议吗?
...谢谢
答案 0 :(得分:2)
我不明白这一行:
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();
}
答案 1 :(得分:0)
怎么样:
return LGDataBase.rawquery("select STATE_NAME from State_Master", null);
更新
您可能需要遍历光标以将所有值输出到这样的数组:How can I create a list Array with the cursor data in Android
ArrayList<WhateverTypeYouWant> mArrayList = new ArrayList<WhateverTypeYouWant>();
for(mCursor.moveToFirst(); mCursor.moveToNext(); mCursor.isAfterLast()) {
// The Cursor is now set to the right position
mArrayList.add(mCursor.getWhateverTypeYouWant(WHATEVER_COLUMN_INDEX_YOU_WANT));
}