使用我的数据库的LogIn类:
package edu.flying.panda.taskmanager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
/**
* Login Screen
* @author FlyingPanda
*/
public class LogIn extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in);
final DatabaseHandler userDB = new DatabaseHandler(this);
final Button logIn_button = (Button) findViewById(R.id.log_in_logIn_button);
final Button signUp_button = (Button) findViewById(R.id.log_in_register_button);
/**
* Listener for LogIn button, checks for correct Username/Password and handles errors
*/
logIn_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.log_in_username);
EditText password = (EditText) findViewById(R.id.log_in_pass);
TextView errorText = (TextView) findViewById(R.id.log_in_error);
//check credentials
User user = userDB.getUser(username.getText().toString());
//error^
Log.d("cc", "user is found");
if (user!=null){
Log.d("cc", "user is not null");
if (user.getPassword().equals(password)){
Log.d("cc", "pass correct");
}else {
errorText.setText("Username/Password incorrect");
}
}
}
});
}
我的databaseHelper类,它使用SQLite
package edu.flying.panda.taskmanager;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper{
//db version
private static final int DATABASE_VERSION = 1;
//db name
private static final String DATABASE_NAME = "userManager";
//table name
private static final String TABLE_USERS = "users";
//user table column names
private static final String KEY_ID = "id";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
//constructor
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USERS_TABLE = "CREATE TABLE" + TABLE_USERS + "(" + KEY_ID +
"INTEGER PRIMARY KEY," + KEY_USERNAME + "TEXT," + KEY_PASSWORD + "TEXT,"
+ KEY_NAME + "TEXT," + KEY_EMAIL + "TEXT" + ")";
db.execSQL(CREATE_USERS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
public void addUser(User user){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME, user.getUsername());
values.put(KEY_PASSWORD, user.getPassword());
values.put(KEY_NAME, user.getName());
values.put(KEY_EMAIL, user.getEmail());
//inserting row
db.insert(TABLE_USERS, null, values);
db.close();
}
//get user by id
public User getUser(int id){
SQLiteDatabase db = this.getReadableDatabase();
//cursor returned will only be one row of the table, with the matching id
Cursor cursor = db.query(TABLE_USERS,
new String[]{KEY_ID, KEY_USERNAME, KEY_PASSWORD, KEY_NAME, KEY_EMAIL},
KEY_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor !=null) cursor.moveToFirst();
User user = new User(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3), cursor.getString(4));
return user;
}
public User getUser(String username){
SQLiteDatabase db = this.getReadableDatabase();
//cursor returned will only be one row of the table, with the matching id
Cursor cursor = db.query(TABLE_USERS,
new String[]{KEY_ID, KEY_USERNAME , KEY_PASSWORD, KEY_NAME, KEY_EMAIL},
KEY_USERNAME + "=?", new String[]{username}, null, null, null, null);
if (cursor !=null) cursor.moveToFirst();
User user = new User(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3), cursor.getString(4));
return user;
}
public List<User> getAllUsers(){
List<User> userList = new ArrayList<User>();
//select all query
String selectQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getReadableDatabase();
//just a cursor that points to the user table TABLE_USERS
Cursor cursor = db.rawQuery(selectQuery, null);
//adding users to list
if(cursor.moveToFirst()){
do{
User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setUsername(cursor.getString(1));
user.setPassword(cursor.getString(2));
user.setName(cursor.getString(3));
user.setEmail(cursor.getString(4));
userList.add(user);
}while(cursor.moveToNext());
}
return userList;
}
logcat的:
03-31 22:26:33.613: I/SqliteDatabaseCpp(538): sqlite returned: error code = 1, msg = near "TABLEusers": syntax error, db=/data/data/edu.flying.panda.taskmanager/databases/userManager
03-31 22:26:33.653: E/SQLiteOpenHelper(538): Couldn't open userManager for writing (will try read-only):
03-31 22:26:33.653: E/SQLiteOpenHelper(538): android.database.sqlite.SQLiteException: near "TABLEusers": syntax error: , while compiling: CREATE TABLEusers(idINTEGER PRIMARY KEY,usernameTEXT,passwordTEXT,nameTEXT,emailTEXT)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at edu.flying.panda.taskmanager.DatabaseHandler.onCreate(DatabaseHandler.java:37)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at edu.flying.panda.taskmanager.DatabaseHandler.getUser(DatabaseHandler.java:78)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at edu.flying.panda.taskmanager.LogIn$1.onClick(LogIn.java:55)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.view.View.performClick(View.java:3511)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.view.View$PerformClick.run(View.java:14105)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.os.Handler.handleCallback(Handler.java:605)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.os.Looper.loop(Looper.java:137)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-31 22:26:33.653: E/SQLiteOpenHelper(538): at dalvik.system.NativeStart.main(Native Method)
03-31 22:26:33.683: D/AndroidRuntime(538): Shutting down VM
03-31 22:26:33.683: W/dalvikvm(538): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-31 22:26:33.713: E/AndroidRuntime(538): FATAL EXCEPTION: main
03-31 22:26:33.713: E/AndroidRuntime(538): android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: /data/data/edu.flying.panda.taskmanager/databases/userManager
03-31 22:26:33.713: E/AndroidRuntime(538): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:244)
03-31 22:26:33.713: E/AndroidRuntime(538): at edu.flying.panda.taskmanager.DatabaseHandler.getUser(DatabaseHandler.java:78)
03-31 22:26:33.713: E/AndroidRuntime(538): at edu.flying.panda.taskmanager.LogIn$1.onClick(LogIn.java:55)
03-31 22:26:33.713: E/AndroidRuntime(538): at android.view.View.performClick(View.java:3511)
03-31 22:26:33.713: E/AndroidRuntime(538): at android.view.View$PerformClick.run(View.java:14105)
03-31 22:26:33.713: E/AndroidRuntime(538): at android.os.Handler.handleCallback(Handler.java:605)
03-31 22:26:33.713: E/AndroidRuntime(538): at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 22:26:33.713: E/AndroidRuntime(538): at android.os.Looper.loop(Looper.java:137)
03-31 22:26:33.713: E/AndroidRuntime(538): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-31 22:26:33.713: E/AndroidRuntime(538): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 22:26:33.713: E/AndroidRuntime(538): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 22:26:33.713: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-31 22:26:33.713: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-31 22:26:33.713: E/AndroidRuntime(538): at dalvik.system.NativeStart.main(Native Method)
我使用Logcat来检查程序在到达错误之前得到了多远。 我在其失败的代码行下评论了// error ^。
该代码行是:User user = userDB.getUser(username.getText()。toString());
我对SQLite相当新,所以这个语法错误真的让我失望了。
我认为问题在于我如何创建我的表格。但我不知道。 谢谢你的帮助。
答案 0 :(得分:3)
您需要在创建表查询中包含适当的间距:
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "(" + KEY_ID +
" INTEGER PRIMARY KEY," + KEY_USERNAME + " TEXT," + KEY_PASSWORD + " TEXT,"
+ KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT" + ");";
为了更好地使用,最好使用String.format
:
String query = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY,
%s TEXT, %s TEXT, %s TEXT, %s TEXT);", TABLE_USERS, KEY_ID, KEY_USERNAME,
KEY_PASSWORD, KEY_NAME, KEY_EMAIL);