我有一个问题,无法从我的数据库sqlite显示listview,日志错误是: 引起:android.database.sqlite.SQLiteException:没有这样的表:tb_gejala(代码1):,同时编译:SELECT nama_gejala。在我的浏览器sqlite中,此查询已成功运行。 我的代码有什么问题?
这是我的MainActivity:
MyCustomAdapter dataAdapter = null;
DatabaseHelperInfopenyakit db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHelperInfopenyakit(this);
setContentView(R.layout.main_deteksi);
Button myButton = (Button) findViewById(R.id.button1);
myButton.setOnClickListener(new OnClickListener() {
// public AbsListView gejalaList;
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("Gejala Yang dipilih adalah");
ArrayList<Gejala> gejalaList = dataAdapter.gejalaList;
for (int i = 0; i < gejalaList.size(); i++) {
Gejala gejala = gejalaList.get(i);
if (gejala.isSelected()) {
responseText.append("\n" + gejala.getName());
}
}
Toast.makeText(getApplicationContext(), responseText,
Toast.LENGTH_SHORT).show();
}
});
displayListView();
}
// Mendeklarasikan arraylist gejalaList dan menginisialiasai dengan data dr db
private ArrayList <Gejala> displayListView() {
ArrayList<Gejala> gejalaList = new ArrayList<Gejala>();
SQLiteDatabase sd = db.getReadableDatabase();
Cursor cursor = sd.rawQuery("SELECT nama_gejala FROM tb_gejala", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
gejalaList.add(new Gejala(cursor.getString(0)));
cursor.moveToNext();
}
cursor.close();
// Buata array adapter dari data gejalaList
dataAdapter = new MyCustomAdapter(this, R.layout.gejala_row, gejalaList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Gejala gejala = (Gejala) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Clicked on Row: " + gejala.getName(),
Toast.LENGTH_SHORT).show();
}
});
return gejalaList;
}
private class MyCustomAdapter extends ArrayAdapter<Gejala> {
private ArrayList<Gejala> gejalaList;
public MyCustomAdapter(Context context, int textViewResourceId,
List<Gejala> gejalaList) {
super(context, textViewResourceId, gejalaList);
this.gejalaList = new ArrayList<Gejala>();
this.gejalaList.addAll(gejalaList);
}
private class ViewHolder {
TextView id;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.gejala_row, null);
holder = new ViewHolder();
// holder.id = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Gejala gejala = (Gejala) cb.getTag();
Toast.makeText(
getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_SHORT)
.show();
gejala.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Gejala gejala = gejalaList.get(position);
// holder.id.setText(" (id:" + gejala.getId() + ")");
holder.name.setText(gejala.getName());
holder.name.setChecked(gejala.isSelected());
holder.name.setTag(gejala);
return convertView;
}
}
这是我的DatabaseHelperInfoPenyakit:
private static String DB_PATH = "";
private static String DB_NAME = "sistempakar.sqilte3";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DatabaseHelperInfopenyakit(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH= myContext.getDatabasePath(DB_NAME).toString();
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
db_Read = this.getReadableDatabase();
db_Read.close();
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(){
// this.getReadableDatabase();
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH ;
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 ;
//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 ;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion>oldVersion){
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
我想你可能已经解决了这个问题,但我想提供解决方案来帮助其他人面对这个问题
问题:U最初可能为DB_TABLE使用了不同的名称,因此安装更新时android不会删除包含DB的应用程序数据。(OnUpgrade你只是复制现有的数据库)
简单解决方案:卸载现有应用程序并重新安装。
复杂解决方案:在DBManager中添加逻辑以创建DB_Table(如果它不存在)