运行时错误:SQLiteException:靠近“contact”:语法错误app停止工作

时间:2017-06-29 15:08:37

标签: java android android-sqlite fatal-error

我已经使用SQlite数据库创建了一个注册活动,当我点击符号按钮时,我的应用程序中出现运行时错误.Stacktrace引导我进入我为数据库创建的DatabaseHelper类中的第36行,然后显示注册活动中的错误,但我无法解决它们。任何帮助和想法都非常有用

PS(我在Android开发和stackoverflow中的新功能很抱歉很长的帖子,也许是明显的错误 这是stacktrace

E/SQLiteLog: (1) near "contact": syntax error
06-29 17:48:21.680 5897-5897/kanepraktiki2017.xyz.mreprogramming.kanepraktiki E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                Process: kanepraktiki2017.xyz.mreprogramming.kanepraktiki, PID: 5897
                                                                                                java.lang.IllegalStateException: Could not execute method of the activity
                                                                                                    at android.view.View$1.onClick(View.java:4240)
                                                                                                    at android.view.View.performClick(View.java:5184)
                                                                                                    at android.view.View$PerformClick.run(View.java:20893)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
                                                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                    at android.view.View$1.onClick(View.java:4235)
                                                                                                    at android.view.View.performClick(View.java:5184) 
                                                                                                    at android.view.View$PerformClick.run(View.java:20893) 
                                                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                                    at android.os.Looper.loop(Looper.java:145) 
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5938) 
                                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
                                                                                                 Caused by: android.database.sqlite.SQLiteException: near "contact": syntax error (code 1): , while compiling: create contact table(id integer primary key not null , name text not null, email text not null , username text not null, password text not null);
                                                                                                    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                                    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
                                                                                                    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
                                                                                                    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                                    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                                                    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                                    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1798)
                                                                                                    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
                                                                                                    at kanepraktiki2017.xyz.mreprogramming.kanepraktiki.DabaseHelper.onCreate(DabaseHelper.java:36)
                                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                                    at kanepraktiki2017.xyz.mreprogramming.kanepraktiki.DabaseHelper.insertContact(DabaseHelper.java:41)
                                                                                                    at kanepraktiki2017.xyz.mreprogramming.kanepraktiki.Signup.onSignUpClick(Signup.java:49)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                                    at android.view.View$1.onClick(View.java:4235) 
                                                                                                    at android.view.View.performClick(View.java:5184) 
                                                                                                    at android.view.View$PerformClick.run(View.java:20893) 
                                                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                                    at android.os.Looper.loop(Looper.java:145)

DatabaseHelper.java代码$

Public class DabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_name = "contacts.db";
private static final String TABLE_name = "contacts";
private static final String COLUMN_id = "id";
private static final String COLUMN_name = "name";
private static final String COLUMN_email = "email";
private static final String COLUMN_username = "username";
private static final String COLUMN_password = "password";
SQLiteDatabase db;

private static final String TABLE_create = "create contact table(id integer primary key not null , " +
        "name text not null, email text not null , username text not null, password text not null);";

public DabaseHelper(Context context) {
    super(context, DATABASE_name, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(TABLE_create);
    this.db = db;
}

public void insertContact(Contact c) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query="select * from contacts";
    Cursor cursor=db.rawQuery(query, null);
    int count=cursor.getCount();

    values.put(COLUMN_id, count);
    values.put(COLUMN_name, c.getName());
    values.put(COLUMN_email, c.getEmail());
    values.put(COLUMN_username, c.getUsername());
    values.put(COLUMN_password, c.getPassword());

    db.insert(DATABASE_name, null, values);
    db.close();
}

public String searchpass(String username) {
    db = this.getReadableDatabase();
    String query = "select username, password from" + TABLE_name;
    Cursor cursor = db.rawQuery(query, null);
    String a, b;
    b = "Δεν βρέθηκε";

    if (cursor.moveToFirst()) {
        do {
            a = cursor.getString(0);
            if (a.equals(username)) {
                b = cursor.getString(1);
                break;
            }
        }
        while (cursor.moveToNext());
    }
     return b;
}


@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    String query ="Drop Table If Exists"+TABLE_name;
    db.execSQL(query);
    this.onCreate(db);

}
}

2 个答案:

答案 0 :(得分:1)

创建新表的SQL语句无效。您可以切换关键字table和表名。试试这个:

CREATE TABLE contact 
(
   id integer primary key not null,
   name text not null, 
   email text not null, 
   username text not null, 
   password text not null
);

此声明中也有失败。

 String query="select * from contacts";

您可以使用变量TABLE_name,但必须更改表的名称。

private static final String TABLE_name = "contact";

然后像这样更改SQL语句:

String query = "SELECT * FROM" + this.TABLE_name;

答案 1 :(得分:0)

将您的陈述改为此陈述:

"create table contact(id integer primary key not null , " +
        "name text not null, email text not null , username text not null, password text not null);"

而不是“创建联系表(...)”,它应该是“创建表联系人(...)”

编辑:如果您仍然收到错误更改此行:

 String query="select * from contacts";

到此:

 String query="select * from contact";

此外,如果您不断收到错误,可能是因为您正在尝试重新创建已创建的表。请学习SQL,SQLite和Andriod数据库连接,以便了解它的实际工作原理。互联网研究可以帮助您理解像这些

这样的高度记录的主题