这实际上是我第一次尝试在 android 上使用 SQL 数据库。所以我希望你能帮助我。我目前正在为我们的游戏开发一个引导条,并想为它编写通信类,但我遇到了麻烦。我为此使用了http://www.vogella.com/articles/AndroidSQLite/article.html。这就是我的 Db Helper 类的样子
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_LEADBOARD = "leadboard";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_LEVEL = "level";
public static final String COLUMN_KILLPOINTS = "killpoints";
private static final String DATABASE_NAME = "leadboard.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_LEADBOARD + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME
+ " text,"+ COLUMN_LEVEL + " integer," +COLUMN_KILLPOINTS + " integer);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LEADBOARD);
onCreate(db);
}
}
我写了一个简单的元素,它有 4 个字段和 getter 和 setter
public class LeadbordElement {
private long id;
private int level;
private int killPoints;
private String name;
我的连接类现在看起来像这样
public class DatabaseCommunication {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
// all columns for the query
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_LEVEL,
MySQLiteHelper.COLUMN_KILLPOINTS };
public DatabaseCommunication(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
/**
* Save element to database
*
* @author Benjamin M. 17.01.2013
* @param element
*/
public void addLeadbordElement(LeadbordElement element) {
this.open();
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, element.getName());
values.put(MySQLiteHelper.COLUMN_LEVEL, element.getLevel());
values.put(MySQLiteHelper.COLUMN_KILLPOINTS, element.getKillPoints());
database.insert(MySQLiteHelper.TABLE_LEADBOARD, null, values);
this.close();
}
/**
* Methode to get a Vektor of elements
*
* @author Benjamin M. 17.01.2013
*/
public Vector<LeadbordElement> getTop10() {
this.open();
// should return ordert by killpoints
Cursor cursor = database.query(MySQLiteHelper.TABLE_LEADBOARD,
allColumns, null, null, null, null,
MySQLiteHelper.COLUMN_KILLPOINTS + " DESC");
Vector<LeadbordElement> elements = new Vector<LeadbordElement>();
//creating the vector of elements
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
elements.add(this.createElement(cursor));
cursor.moveToNext();
}
cursor.close();
this.close();
return elements;
}
/**
* Methode to create a LeadbordElement out of the cursor
*
* @author Benjamin M. 17.01.2013
* @param cursor
* @return
*/
private LeadbordElement createElement(Cursor cursor) {
LeadbordElement element = new LeadbordElement();
element.setId(cursor.getLong(0));
element.setName(cursor.getString(1));
element.setLevel(cursor.getInt(2));
element.setKillPoints(cursor.getInt(3));
return element;
}
}
但这实际上是行不通的。我不能像这样进入前10名,我也没有弄错。好吧,实际上我可以创建 DatabaseCommunication 并且可以添加一个元素。不确定它是否真的被添加到数据库中。但我不能让那些回来。它说没有 Column: level: ,而编译时: select _id .......
至少我有一个 textview 并想在那里显示元素。
谢谢,如果你能帮忙!
错误日志:
01-17 19:04:35.920: E/AndroidRuntime(1428): FATAL EXCEPTION: main
01-17 19:04:35.920: E/AndroidRuntime(1428): java.lang.IllegalStateException: Could not execute method of the activity
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.view.View$1.onClick(View.java:3044)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.view.View.performClick(View.java:3511)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.view.View$PerformClick.run(View.java:14110)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.os.Handler.handleCallback(Handler.java:605)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.os.Looper.loop(Looper.java:137)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-17 19:04:35.920: E/AndroidRuntime(1428): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 19:04:35.920: E/AndroidRuntime(1428): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 19:04:35.920: E/AndroidRuntime(1428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 19:04:35.920: E/AndroidRuntime(1428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 19:04:35.920: E/AndroidRuntime(1428): at dalvik.system.NativeStart.main(Native Method)
01-17 19:04:35.920: E/AndroidRuntime(1428): Caused by: java.lang.reflect.InvocationTargetException
01-17 19:04:35.920: E/AndroidRuntime(1428): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 19:04:35.920: E/AndroidRuntime(1428): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.view.View$1.onClick(View.java:3039)
01-17 19:04:35.920: E/AndroidRuntime(1428): ... 11 more
01-17 19:04:35.920: E/AndroidRuntime(1428): Caused by: android.database.sqlite.SQLiteException: no such column: level: , while compiling: SELECT _id, name, level, killpoints FROM leadboard ORDER BY killpoints DESC
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
01-17 19:04:35.920: E/AndroidRuntime(1428): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
01-17 19:04:35.920: E/AndroidRuntime(1428): at control.database.DatabaseCommunication.getTop10(DatabaseCommunication.java:61)
01-17 19:04:35.920: E/AndroidRuntime(1428): at com.main.MainActivity.startLeadbord(MainActivity.java:178)
01-17 19:04:35.920: E/AndroidRuntime(1428): ... 14 more