我正在开发一个在 SQLite 中保存登录用户信息的 android 应用程序。但我面临着奇怪的问题。在我的 DBHelper 类中创建表函数创建得很好,插入函数不会引发任何异常,但是当我尝试从我的 SQL Lite DB 检索数据时它会引发异常
没有这样的列:officer(Sqlite 代码 1):,编译时:Select * from User where userId=officer,(操作系统错误 - 2:没有这样的文件或目录)
我尝试多次更改数据库名称和版本,但没有任何效果。我试图从我的 android 设备中查看数据库文件,以确保我的表创建是否正常,但是当我在手机的 Android/data 文件夹中搜索时,所有应用程序包名称都显示在那里,但我的应用程序的包名称不存在。
public class DBHelper extends SQLiteOpenHelper {
// Database Name
private static final String DATABASE_NAME = "CMDB.db";
private static final int DATABASE_VERSION = 2;
// Contacts table name
private static final String TABLE_USER = "User";
public static DBHelper dbHelper;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static DBHelper getInstance(Context context) {
if (dbHelper == null) {
dbHelper = new DBHelper(context.getApplicationContext());
}
return dbHelper;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
createTables(sqLiteDatabase);
Toast.makeText(App.context,"onCreate",Toast.LENGTH_SHORT).show();
}
private void createTables(SQLiteDatabase sqLiteDatabase) {
try {
sqLiteDatabase.execSQL(" CREATE TABLE " + TABLE_USER + " (" +
"userId" + " TEXT PRIMARY KEY," + "userImage" + " TEXT NOT NULL," +
"userRole" + " TEXT NOT NULL," + "token" + " TEXT NOT NULL," + "userName" + " TEXT NOT NULL," + "userEmail" + " TEXT NOT NULL," + "userAddress" + " TEXT NOT NULL," + "mobile" + " TEXT NOT NULL);"
);
Toast.makeText(App.context,"createTables",Toast.LENGTH_SHORT).show();
}catch (Exception ex){
Toast.makeText(App.context,ex.getMessage(),Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
dropTables(sqLiteDatabase);
// Create tables again
onCreate(sqLiteDatabase);
}
private void dropTables(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
}
public void saveUserToDB(User user) {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("userId", user.userID);
values.put("userImage", user.userImage);
values.put("userRole", user.userRole);
values.put("token", user.token);
values.put("UserName", user.UserName);
values.put("userEmail", user.userEmail);
values.put("userAddress", user.userAddress);
values.put("mobile", user.mobile);
db.insert(TABLE_USER, null, values);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public User getUserFromDB(String userid) {
User user = new User();
try {
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + TABLE_USER + " where userId=" +userid+ "";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
user.userID = cursor.getString(0);
user.userImage = cursor.getString(1);
user.userRole = cursor.getString(2);
user.token = cursor.getString(3);
user.UserName = cursor.getString(4);
user.userEmail = cursor.getString(5);
user.userAddress = cursor.getString(6);
user.mobile = cursor.getString(7);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return user;
}
public void updateUser(User user, String userId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
Gson gson = new Gson();
String userJson = gson.toJson(user);
contentValues.put("user", userJson);
db.update(TABLE_USER, contentValues, "userId=" + userId, null);
}
public void updateUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("userId", user.userID);
cv.put("userImage", user.userImage);
cv.put("userRole", user.userRole);
cv.put("token", user.token);
cv.put("UserName", user.UserName);
cv.put("userEmail", user.userEmail);
cv.put("userAddress", user.userAddress);
cv.put("mobile", user.mobile);
db.update(TABLE_USER, cv, "userId=" + user.userID, null);
}}
我希望使用用户表和提到的列创建 SQLite DB。还需要在 Android Device 中查看 .db 文件。