我正在尝试在ListView中显示来自SQLiteDatabase的数据,但是我收到错误: 引起:android.database.sqlite.SQLiteException:没有这样的表:Studenten(代码1):,编译时:DELETE FROM Studenten
这是我的代码(来自StudentDBAdapter):
package com.ipmedt4.challengeweek_v2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Charlie on 28-12-2014.
*/
public class StudentDBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAAM = "_naam";
public static final String KEY_STUDENTNUMMER = "_studentnummer";
public static final String KEY_KLAS = "_klas";
public static final String KEY_CIJFER = "_cijfer";
public static final String KEY_OPMERKINGEN = "_opmerkingen";
private static final String TAG = "StudentDBAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDB;
private static final String DATABASE_NAME = "Challengeweek";
private static final String SQLITE_TABLE = "Studenten";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS" + SQLITE_TABLE + "(" +
KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAAM + "," + KEY_STUDENTNUMMER + ","
+ KEY_KLAS + "," + KEY_CIJFER + "," + KEY_OPMERKINGEN + "," + ")";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
Log.w(TAG, DATABASE_CREATE);
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(TAG, "Upgrading database from version" + oldVersion + "to" + newVersion +
"which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + SQLITE_TABLE);
onCreate(db);
}
}
public StudentDBAdapter (Context ctx){
this.mCtx = ctx;
}
public StudentDBAdapter open () throws SQLiteException{
mDbHelper = new DatabaseHelper(mCtx);
mDB = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createStudent (String naam, String studentnummer, String klas){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAAM, naam);
initialValues.put(KEY_STUDENTNUMMER, studentnummer);
initialValues.put(KEY_KLAS, klas);
return mDB.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllStudenten(){
int doneDelete = 0;
doneDelete = mDB.delete(SQLITE_TABLE, null, null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchStudentenbyName(String inputText) throws SQLiteException{
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length() == 0){
mCursor = mDB.query(SQLITE_TABLE, new String[] {
KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS
}, null, null, null, null, null);
}
else {
mCursor = mDB.query(true, SQLITE_TABLE, new String [] {KEY_ROWID,
KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS}, KEY_NAAM + "like '%" + inputText +
"%'", null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor showAlleStudenten(){
Cursor mCursor = mDB.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER,
KEY_KLAS}, null, null, null, null, null);
if (mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
public void insertStudenten(){
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Adel, Pieter", "s1078455", "INF1G");
}
}
这来自实现ListView的类:
package com.ipmedt4.challengeweek_v2;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import java.util.ArrayList;
public class OverzichtStudenten extends Activity {
private StudentDBAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overzicht_studenten);
dbHelper = new StudentDBAdapter(this);
dbHelper.open();
//maak alle data schoon
dbHelper.deleteAllStudenten();
//toevoegen van data
dbHelper.insertStudenten();
//genereren ListView van SQLiteDatabase
displayListView();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void displayListView(){
Cursor cursor = dbHelper.showAlleStudenten();
//selecteer gewenste kolommen
String[] columns = new String[]{
StudentDBAdapter.KEY_NAAM,
StudentDBAdapter.KEY_STUDENTNUMMER,
StudentDBAdapter.KEY_KLAS
};
//In XML gedefiniërde Views
int[] to = new int[]{
R.id.naam,
R.id.studentnummer,
R.id.klas
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.student_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
String studentNaam = cursor.getString(cursor.getColumnIndexOrThrow("naam"));
Toast.makeText(getApplicationContext(),
studentNaam, Toast.LENGTH_SHORT).show();
}
});
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
{ dataAdapter.getFilter().filter(s.toString());
}}
@Override
public void afterTextChanged(Editable s) {
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchStudentenbyName(constraint.toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_overzicht_studenten, menu);
return true;
}
@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);
}
}
我无法弄明白。我希望你能帮助我。
答案 0 :(得分:4)
您实际上从未创建过表格 - 您的SQLiteOpenHelper.onCreate()
为空。