I am trying to implement ListView with a SQLite Database, but I keep getting the error 'Cannot resolve symbol TABLE_STUDENTEN'. 'TABLE_STUDENTEN' is the name of the table in the database. This is my code for the DBHandler:
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "StudentDB";
public static final String TABLE_STUDENTEN = "Studenten";
public static String DB_PATH;
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAAM = "_naam";
public static final String COLUMN_STUDENTNUMMER = "_studentnummer";
public static final String COLUMN_KLAS = "_klas";
public static final String COLUMN_CIJFER = "_cijfer";
public static final String COLUMN_OPMERKINGEN = "_opmerkingen";
private Context currentContext;
public DBHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Context currentContext = context;
DB_PATH = "/data/data/" + context.getPackageName()+ "/databases";
CreateDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void CreateDatabase() {
boolean dbExists = checkDbExists();
if (dbExists) {
// niks doen
} else {
SQLiteDatabase DB = currentContext.openOrCreateDatabase(DATABASE_NAME, 0, null);
DB.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_STUDENTEN +
"(ID INT, naam VARCHAR, studentnummer VARCHAR, klas VARCHAR, cijfer INT, opmerkingen VARCHAR)" );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Abrahamse, Peter', 's1078846', 'INF1D'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Bremen, Karel', 's1087311', 'INF1C'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Dam', Lisa 's1074243', 'INF1G'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Evertsen, Frederik', 's1087599', 'INF1B'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Frederiksen, Willem', 's1071475', 'INF1A'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Greppel, Maria', 's1087412', 'INF1E'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Joker, Dorine', 's1077851', 'INF1F'); " );
DB.execSQL("INSERT INTO" + TABLE_STUDENTEN + "Values ('Knopper, Jan', 's1071288', 'INF1H'); " );
}
}
private boolean checkDbExists(){
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database bestaat nog niet
}
if (checkDB != null); {
checkDB.close();
}
return checkDB != null ? true : false;
}
}
And this is the code for the class:
package com.ipmedt4.challengeweek_v2;
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.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import java.util.ArrayList;
public class OverzichtStudenten extends ListActivity {
private ArrayList<String> allestudentenresultaat = new ArrayList<String>();
private SQLiteDatabase StudentDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overzicht_studenten);
openAndQueryDatabase();
displayResultatenLijst();
}
private void displayResultatenLijst(){
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.activity_list_item, allestudentenresultaat));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase(){
try {
DBHandler dbHandler = new DBHandler(this.getApplicationContext());
StudentDB = dbHandler.getWritableDatabase();
Cursor c = StudentDB.rawQuery("SELECT naam, studentnummer, klas FROM" + TABLE_STUDENTEN, null);
if (c != null && c.moveToFirst()) {
do {
String naam = c.getString(c.getColumnIndex("naam" ));
String studentnummer = c.getString(c.getColumnIndex("studentnummer" ));
String klas = c.getString(c.getColumnIndex("klas" ));
}
while (c.moveToNext());
}
}
catch (SQLiteException se){
Log.e(getClass().getSimpleName(), "Kon database niet maken of openen");
}
finally {
if (StudentDB != null)
StudentDB.execSQL("DELETE FROM" + StudentDB);
StudentDB.close();
}
}
@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);
}
}
Sorry for the weird names, I am Dutch. The class DBHandler contains no errors. The only error I cannot resolve is at the line 'Cursor c = StudentDB.rawQuery("SELECT naam, studentnummer, klas FROM" + TABLE_STUDENTEN, null);' The error is 'cannot resolve symbol TABLE_STUDENTEN'
Hope you can help me. Thanks!
Aucun commentaire:
Enregistrer un commentaire