Runtime Error :SQLiteException: near "contact": syntax error app stopped working
Runtime Error :SQLiteException: near "contact": syntax error app stopped working
我已经使用 SQlite 数据库创建了一个注册 activity,当我点击标志 button.Stacktrace 时我的应用程序出现运行时错误导致我进入 [=25= 中的第 36 行] 我为数据库创建的 DatabaseHelper 然后在注册时显示错误 activity 但我无法解决它们。任何帮助和想法都会非常有用
PS(我是 android 开发人员和 Whosebug 的新手,很抱歉 post 以及明显的错误
这是堆栈跟踪
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.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.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.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);
}
}
将您的声明更改为:
"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);"
而不是 "create contact table(...)" 应该是 "create table contact(...)"
编辑:如果您仍然遇到错误,请更改此行:
String query="select * from contacts";
对此:
String query="select * from contact";
此外,如果您不断收到错误,可能是因为您正在尝试重新创建一个已经创建的 table。请去学习 SQL、SQLite 和 Andriod 数据库连接,以便您了解它的实际工作原理。互联网研究可以大大帮助您理解像这些有据可查的主题
您创建新 table 的 SQL 语句无效。您必须切换关键字 table
和 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,但必须更改 table 的名称。
private static final String TABLE_name = "contact";
然后像这样更改您的 SQL 语句:
String query = "SELECT * FROM" + this.TABLE_name;
我已经使用 SQlite 数据库创建了一个注册 activity,当我点击标志 button.Stacktrace 时我的应用程序出现运行时错误导致我进入 [=25= 中的第 36 行] 我为数据库创建的 DatabaseHelper 然后在注册时显示错误 activity 但我无法解决它们。任何帮助和想法都会非常有用
PS(我是 android 开发人员和 Whosebug 的新手,很抱歉 post 以及明显的错误
这是堆栈跟踪
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.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.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.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);
}
}
将您的声明更改为:
"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);"
而不是 "create contact table(...)" 应该是 "create table contact(...)"
编辑:如果您仍然遇到错误,请更改此行:
String query="select * from contacts";
对此:
String query="select * from contact";
此外,如果您不断收到错误,可能是因为您正在尝试重新创建一个已经创建的 table。请去学习 SQL、SQLite 和 Andriod 数据库连接,以便您了解它的实际工作原理。互联网研究可以大大帮助您理解像这些有据可查的主题
您创建新 table 的 SQL 语句无效。您必须切换关键字 table
和 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,但必须更改 table 的名称。
private static final String TABLE_name = "contact";
然后像这样更改您的 SQL 语句:
String query = "SELECT * FROM" + this.TABLE_name;