我正在尝试创建SQLiteDatabase那个功能,以便在表不为null时保持用户登录,如果它为null,则活动将请求用户和密码,并且表首先有两行是Name,第二行是position。但我不知道为什么它会给我这个错误:
02-27 09:53:22.142 11519-11519/com.amaiub.hussain.smartmenu E/SQLiteLog: (1) no such table: User_Table
02-27 09:53:22.149 11519-11519/com.amaiub.hussain.smartmenu E/SQLiteDatabase: Error inserting user_position=admin user_name=hussain
android.database.sqlite.SQLiteException: no such table: User_Table (code 1): , while compiling: INSERT INTO User_Table(user_position,user_name) VALUES (?,?)
我在数据库中创建了这个表
public class DatabaseHelper extends SQLiteOpenHelper {
private final static String DB_NAME = "e-Order";
private final static int DB_VERSION = 1;
private final String USER_TABLE = "User_Table";
private String USER="user_name",POSITION="user_position";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE '" + USER_TABLE + "'('" + USER + "'TEXT,'" + POSITION + "'TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS '" + USER_TABLE + "'");
onCreate(db);
}
public int logIn(String userName, String userPosition) {
int result = 0;
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(USER, userName);
cv.put(POSITION, userPosition);
result = (int) db.insert(USER_TABLE, null, cv);
if (db != null)
db.close();
return result;
}
public int getUser() {
int total = 0;
SQLiteDatabase db = getReadableDatabase();
Cursor cur = db.query(USER_TABLE, null, null, null, null, null, null);
if (cur.getCount() > 0)
total = cur.getCount();
if (cur != null)
cur.close();
if (db != null)
db.close();
return total;
}
public int logOut() {
int result = 0;
SQLiteDatabase db = getWritableDatabase();
result = db.delete(USER_TABLE, null, null);
if (db != null)
db.close();
return result;
}
并在登录活动中
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("user")||jsonObject.names().get(0).equals("position")) {
Toast.makeText(getApplicationContext(), "Welcome "+jsonObject.getString("position"), Toast.LENGTH_SHORT).show();
user_name=jsonObject.getString("user");
user_position=jsonObject.getString("position");
if (user_name!=null||user_position!=null) {
helper.logIn(user_name, user_position);
finish();
Intent intent = new Intent(getApplicationContext(), MainPage.class);
intent.putExtra("user", user_name);
intent.putExtra("position", user_position);
startActivity(intent);
}
} else {
sign_in.setError("");
Toast.makeText(getApplicationContext(), "Error" + jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
creatingAnime();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
我不知道是什么问题
答案 0 :(得分:0)
尝试使用以下查询:
"CREATE TABLE " + USER_TABLE + "(" + USER + " TEXT, " + POSITION + " TEXT )";
似乎需要在表创建时进行更正。这里看一下create table语法。 link
在进行上述更改和测试之前,不要忘记卸载以前的应用。
快乐编码......
答案 1 :(得分:0)
您似乎在创建脚本db.execSQL("CREATE TABLE '" + USER_TABLE + "'('" + USER + "'TEXT,'" + POSITION + "'TEXT)");
尝试在输入类似内容之前添加空格:
db.execSQL("CREATE TABLE " + USER_TABLE + " (" + USER + " TEXT, " + POSITION + " TEXT)");
答案 2 :(得分:0)
这样做:
1º在值SQLiteDatabase db;
下添加2º
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + USER_TABLE + "("
+ USER + " TEXT,"
+ POSITION + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}