我的旧 SQLite 数据库没有按照我想要的方式工作,所以我想删除我的旧 DBAdapter 文件夹和 DBHelper 文件夹,并从头开始创建一个新数据库,其中包含 2 个新表、新列名和一个新 DATABASE_NAME 但之后我使用新的字符串和光标将所有内容链接到我的应用程序我遇到了空点崩溃,并且在我的 logcat 中遇到了这些错误。
更新:我已将 onCreate 参数更改为 db 并解决了无效表错误,但现在我有一个 Table "Players"(MyFirstTable) 不存在错误。
更新:新的 LOGCAT
04-19 19:56:02.781: E/AndroidRuntime(9179): FATAL EXCEPTION: main
04-19 19:56:02.781: E/AndroidRuntime(9179): android.database.sqlite.SQLiteException: no such table: PLAYERS (code 1): , while compiling: SELECT _id, USERNAME FROM PLAYERS
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
04-19 19:56:02.781: E/AndroidRuntime(9179): at com.fullfrontalgames.numberfighter.DBAdapter.getUsername(DBAdapter.java:143)
04-19 19:56:02.781: E/AndroidRuntime(9179): at com.fullfrontalgames.numberfighter.Findfriends$1.onClick(Findfriends.java:39)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.view.View.performClick(View.java:4106)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.view.View$PerformClick.run(View.java:17052)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.os.Handler.handleCallback(Handler.java:615)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.os.Handler.dispatchMessage(Handler.java:92)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.os.Looper.loop(Looper.java:137)
04-19 19:56:02.781: E/AndroidRuntime(9179): at android.app.ActivityThread.main(ActivityThread.java:5059)
04-19 19:56:02.781: E/AndroidRuntime(9179): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 19:56:02.781: E/AndroidRuntime(9179): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 19:56:02.781: E/AndroidRuntime(9179): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-19 19:56:02.781: E/AndroidRuntime(9179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-19 19:56:02.781: E/AndroidRuntime(9179): at dalvik.system.NativeStart.main(Native Method)
DBAdapter 类
package com.fullfrontalgames.numberfighter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_USERNAME = "USERNAME";
static final String KEY_PASSWORD = "PASSWORD";
static final String KEY_EMAIL = "EMAIL";
static final String KEY_NUMBERINPUT = "NUMBERINPUT";
static final String KEY_SCORE = "SCORE";
static final String KEY_FRIENDS = "FRIENDS";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "NFDatabase";
static final String DATABASE_TABLE1 = "PLAYERS";
static final String DATABASE_TABLE2 = "FRIENDSLIST";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE_TABLE1 = "create table PLAYERS (_id integer primary key autoincrement,"
+ "USERNAME text not null,PASSWORD text not null,EMAIL text not null,NUMBERINPUT text not null,,SCORE text not null;";
static final String DATBASE_CREATE_TABLE2 = "create table FRIENDSLIST (_id integer primary key autoincrement,"
+ "FRIENDS text not null,USERNAME text not null,NUMBERINPUT text not null,SCORE text not null;";
final Context context;
DatabaseHelper DBHelper;
static SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(DATABASE_CREATE_TABLE1);
db.execSQL(DATABASE_CREATE_TABLE2);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS PLAYERS,FRIENDSLIST");
onCreate(db);
}
}
public DBAdapter open() throws SQLiteException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
// PLAYERS TABLE CRUD
public void insertPlayer(String Username, String Password, String Email) {
ContentValues initialValues = new ContentValues();
initialValues.put("USERNAME", Username);
initialValues.put("PASSWORD", Password);
initialValues.put("EMAIL", Email);
}
public boolean deletePlayer(String username) {
return db.delete("PLAYERS", KEY_ROWID + " = " + "_id", null) > 0;
}
public Cursor getAllPlayers() {
return db.query(null, new String[] {
"_id", "USERNAME", "PASSWORD", "EMAIL"
}, null, null, null, null, null, null);
}
public String getData() {
String[] columns = new String[] {
"_id", "USERNAME", "PASSWORD"
};
Cursor mCursor = db.query("PLAYERS", columns, null, null, null, null, null);
String result = "";
int iRow = mCursor.getColumnIndex(KEY_ROWID);
int iName = mCursor.getColumnIndex(KEY_USERNAME);
for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
result = mCursor.getString(iRow) + " " + mCursor.getString(iName) + "/n";
}
return result;
}
public Cursor getPlayer(long rowid) throws SQLException {
Cursor mCursor = db.query(true, "PLAYERS", new String[] {
"_id", "USERNAME"
}, KEY_ROWID + " = " + "_id", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public String getUsername(String username) {
// TODO Auto-generated method stub
String[] columns = new String[] {
"_id", "USERNAME"
};
Cursor mCursor = db.query("PLAYERS", columns, null, null, null, null, null);
String result = "";
int iRow = mCursor.getColumnIndex(KEY_ROWID);
int iName = mCursor.getColumnIndex(KEY_USERNAME);
for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
result = mCursor.getString(iRow) + " " + mCursor.getString(iName) + "/n";
}
return result;
}
public String getSinlgeEntry(String Username) {
Cursor cursor = db.query("PLAYERS", null, " USERNAME=?", new String[] {
Username
}, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public boolean updatePlayer(long _id, String Username, String Password, String Email) {
ContentValues args = new ContentValues();
args.put("USERNAME", Username);
args.put("PASSWORD", Password);
args.put("EMAIL", Email);
return db.update("PLAYERS", args, KEY_ROWID + " = " + _id, null) > 0;
}
// FRIENDS TABLE CRUD
public void insertFriend(String Friend) {
ContentValues initialValues = new ContentValues();
initialValues.put("FRIENDS", Friend);
db.insert("FRIENDSLIST", null, null);
}
public boolean deleteFriend(String deletedfriend) {
return db.delete("FRIENDSLIST", KEY_ROWID + " = " + "_id", null) > 0;
}
public Cursor getAllFriends() {
return db.query(null, new String[] {
"_id", "FRIENDS"
}, null, null, null, null, null, null);
}
public Cursor getFriend(long rowid) throws SQLException {
Cursor mCursor = db.query(true, "FRIENDSLIST", new String[] {
"_id", "FRIENDS"
}, KEY_ROWID + " = " + "_id", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateFriend(long _id, String Friends) {
ContentValues args = new ContentValues();
args.put("FRIENDS", Friends);
return db.update("FRIENDSLIST", args, KEY_ROWID + " = " + _id, null) > 0;
}
}