我的朋友帮我做这个Android应用程序,但我真的不知道这里的实际问题是什么。你们能帮助我吗?我尝试在android中运行代码,但是当我尝试在我的应用程序登录菜单中登录时,它崩溃了
这是我的数据库助手
package com.example.ikramhs.shoerack;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "login";
private static final int DB_VERSION = 1;
private static final String DB_TABLE = "create table user (id integer primary key autoincrement, " + "username text not null,password text not null;";
public DatabaseHelper(Context context){
super(context, DB_NAME, null,DB_VERSION) ;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade (SQLiteDatabase database, int oldVersion, int newVersion ){
Log.w(DatabaseHelper.class.getName(),
"Upgrading database from version" + oldVersion + "to " + newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS user");
onCreate(database);
}
}
这是我的数据库适配器文件
package com.example.ikramhs.shoerack;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseAdapter {
private static final String LOGIN_TABLE = "user";
public static final String COL_ID = "id";
public static final String COL_USERNAME = "username";
public static final String COL_PASSWORD = "password";
private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
public DatabaseAdapter(Context context) {
this.context = context;
}
public DatabaseAdapter open() {
dbHelper = new DatabaseHelper(context) ;
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public long createUser (String username, String password)
{
ContentValues initialValues = createUserTableContentValues ( username,password );
return database.insert(LOGIN_TABLE,null,initialValues);
}
public Cursor fetchUser(String username, String password)
{
Cursor myCursor = database.query(LOGIN_TABLE, new String[] {COL_ID, COL_USERNAME, COL_PASSWORD},
COL_USERNAME + "=" + username + "'AND" + COL_PASSWORD + "='" + password + "'", null,null,null,null);
if (myCursor != null) {
myCursor.moveToFirst();
}
return myCursor;
}
private ContentValues createUserTableContentValues(String username, String password) {
ContentValues values = new ContentValues();
values.put(COL_USERNAME, username);
values.put(COL_PASSWORD, password);
return values;
}
}
这是Android监视器中的logcat
02-02 01:55:48.439 2659-2659/com.example.ikramhs.shoerack I/art: Not late-enabling -Xcheck:jni (already on)
02-02 01:55:48.585 2659-2659/com.example.ikramhs.shoerack W/System: ClassLoader referenced unknown path: /data/app/com.example.ikramhs.shoerack-2/lib/x86
02-02 01:55:48.639 2659-2672/com.example.ikramhs.shoerack D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-02 01:55:48.725 2659-2672/com.example.ikramhs.shoerack I/OpenGLRenderer: Initialized EGL, version 1.4
02-02 01:55:48.770 2659-2672/com.example.ikramhs.shoerack W/EGL_emulation: eglSurfaceAttrib not implemented
02-02 01:55:48.770 2659-2672/com.example.ikramhs.shoerack W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad920680, error=EGL_SUCCESS
02-02 01:55:49.803 2659-2665/com.example.ikramhs.shoerack W/art: Suspending all threads took: 23.765ms
02-02 01:55:53.288 2659-2672/com.example.ikramhs.shoerack E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab9c45a0
02-02 01:55:54.230 2659-2665/com.example.ikramhs.shoerack W/art: Suspending all threads took: 8.143ms
02-02 01:55:54.294 2659-2672/com.example.ikramhs.shoerack W/EGL_emulation: eglSurfaceAttrib not implemented
02-02 01:55:54.294 2659-2672/com.example.ikramhs.shoerack W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad926e60, error=EGL_SUCCESS
02-02 01:56:08.158 2659-2659/com.example.ikramhs.shoerack E/SQLiteLog: (1) near "'ANDpassword='": syntax error
02-02 01:56:08.159 2659-2659/com.example.ikramhs.shoerack D/AndroidRuntime: Shutting down VM
02-02 01:56:08.159 2659-2659/com.example.ikramhs.shoerack E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ikramhs.shoerack, PID: 2659
android.database.sqlite.SQLiteException: near "'ANDpassword='": syntax error (code 1): , while compiling: SELECT id, username, password FROM user WHERE username=therhaman'ANDpassword='091192'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.ikramhs.shoerack.DatabaseAdapter.fetchUser(DatabaseAdapter.java:42)
at com.example.ikramhs.shoerack.welcome.LogMeIn(welcome.java:68)
at com.example.ikramhs.shoerack.welcome.access$000(welcome.java:17)
at com.example.ikramhs.shoerack.welcome$1.onClick(welcome.java:46)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-02 01:56:09.955 2659-2659/? I/Process: Sending signal. PID: 2659 SIG: 9
答案 0 :(得分:0)
android.database.sqlite.SQLiteException: near "'ANDpassword='": syntax error (code 1): , while compiling: SELECT id, username, password FROM user WHERE username=therhaman'ANDpassword='091192'
您需要AND和密码之间的空格。
答案 1 :(得分:0)
您的Logcat明确表示“接近”'ANDpassword ='“:语法错误(代码1)”。 您有SQL语法错误,我相信它应该是“AND password =”。改变下面的行,使它成为(COL_PASSWORD之后的空格):
COL_USERNAME + "=" + username + "' AND " + COL_PASSWORD + "='" + password + "