我已经在 Android 应用程序中设置了 SQLite 数据库,但为了关注即时问题,将仅引用
Users
表。在 DatabaseContract
中,Users
表定义为:
public final class DatabaseContract {
private DatabaseContract() {}
public static class Users implements BaseColumns {
public static final String TABLE_NAME = "users";
public static final String COLUMN_USER_ID = "_id";
public static final String COLUMN_USER_NAME = "user_name";
public static final String COLUMN_PASSWORD = "password";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_DISPLAY_NAME = "display_name";
}
...rest of tables in db
}
在
DatabaseHelper
课程中,这是Users
表:
private static final String SQL_CREATE_USERS_TABLE =
"CREATE TABLE " + DatabaseContract.Users.TABLE_NAME + " (" +
DatabaseContract.Users.COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
DatabaseContract.Users.COLUMN_USER_NAME + " TEXT NOT NULL, " +
DatabaseContract.Users.COLUMN_PASSWORD + " TEXT NOT NULL, " +
DatabaseContract.Users.COLUMN_EMAIL + " TEXT, " +
DatabaseContract.Users.COLUMN_DISPLAY_NAME + " TEXT)";
在我的
AddUserActivity
类中运行此方法addUserToDatabase(String name, String email, String password, String displayName)
,这似乎有效,但是我希望从Users
表中获取自动生成的“id”,然后在此处检索“id”:
// Check if the insertion was successful
if (newRowId != -1) {
// User was added successfully
Toast.makeText(this, "User added successfully!", Toast.LENGTH_SHORT).show();
// Get the user_id of the newly added user
int user_id = getUserId(db, name);
...
方法如下
getUserId(db, name)
:
private int getUserId(SQLiteDatabase db, String name) {
int user_id = -1; // Default value if no user is found
// Define the columns you want to retrieve
String[] projection = {
DatabaseContract.Users.COLUMN_USER_ID
};
// Define the selection based on the user name
String selection = DatabaseContract.Users.COLUMN_USER_NAME + " = ?";
String[] selectionArgs = { name };
// Define the sort order (if needed)
String sortOrder = null;
Cursor cursor = db.query(
DatabaseContract.Users.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder
);
if (cursor.moveToFirst()) {
// Get the user_id from the cursor
int usersIdIndex = cursor.getColumnIndexOrThrow(DatabaseContract.Users.COLUMN_USER_ID);
user_id = cursor.getInt(usersIdIndex);
}
// Close the cursor (don't close the database here)
cursor.close();
return user_id;
}
然后我创建一个意图将此数据传递到
WelcomeActivity
页面:
...
// Create an Intent to navigate to WelcomeActivity
Intent intent = new Intent(AddUserActivity.this, WelcomeActivity.class);
int testNumber = 777;
// Pass user data and user_id as extras in the Intent
intent.putExtra("USER_ID", user_id);//testNumber
intent.putExtra("USER_NAME", name);
intent.putExtra("USER_EMAIL", email);
intent.putExtra("USER_DISPLAY_NAME", displayName);
// Start the WelcomeActivity
startActivity(intent);
// Close this activity
finish();
此时程序崩溃并出现以下错误:
E/SQLiteLog: (1) no such column: _id in "SELECT _id FROM users WHERE user_name = ?"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.enetapplications.songtracker, PID: 5937
android.database.sqlite.SQLiteException: no such column: _id (code 1 SQLITE_ERROR[1]): , while compiling: SELECT _id FROM users WHERE user_name = ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1478)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:916)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2088)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1935)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1806)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1974)
at com.enetapplications.songtracker.AddUserActivity.getUserId(AddUserActivity.java:124)
at com.enetapplications.songtracker.AddUserActivity.addUserToDatabase(AddUserActivity.java:84)
at com.enetapplications.songtracker.AddUserActivity.access$400(AddUserActivity.java:20)
at com.enetapplications.songtracker.AddUserActivity$1.onClick(AddUserActivity.java:58)
at android.view.View.performClick(View.java:7792)
at android.widget.TextView.performClick(TextView.java:16112)
at android.view.View.performClickInternal(View.java:7769)
at android.view.View.access$3800(View.java:910)
at android.view.View$PerformClick.run(View.java:30218)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8663)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
我是 SQLite 新手,因此适应了 SQLite - 但对标准 SQL/MySQL 非常满意。我的倾向是将
Users
表的键命名为 user_id
,但是 SQLite 标准似乎是将表的键保留为 _id
提前感谢您的审阅和评论!
最好的, 布莱恩
这是一个非常奇怪的问题 - 在应用程序开发的早期,在我的
Users
表中,我将密钥设置为 users_id
但后来更改了它(在 DatabaseHelper
以及 DatabaseContract
到 user_id
。我保留了出现错误并且无法撤消原始数据库配置。最后我能够“看到”数据库元数据,意识到所有这些修改都没有执行任何操作:
public void getTableMetadata(String tableName) {
Log.d("SplashActivityZ", "IN: getTableMetadata(String tableName) - in DatabaseHelper.java, tableName=" + tableName + ")");
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
do {
String columnName = cursor.getString(cursor.getColumnIndex("name"));
String columnType = cursor.getString(cursor.getColumnIndex("type"));
// Log the metadata for each column
Log.d("SplashActivityZ", "Column Name: " + columnName);
Log.d("SplashActivityZ", "Column Type: " + columnType);
} while (cursor.moveToNext());
}
} finally {
cursor.close();
}
}
}
输出始终如一:
D Column Name: users_id
其他尝试包括更改版本:
private static final int DATABASE_VERSION = 3;
也没有做任何事情。
解决方案是更改数据库名称 - 只需添加“2”:
private static final String DATABASE_NAME = "song_tracker2.db";
问题解决了。