我一次又一次地收到这个错误。无法弄清楚如何解决。我正在分享代码。请帮忙!
代码: addnewcontact.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sambal.contactmanager.AddNewContact">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="79dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number:"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email Id:"
android:id="@+id/textView3"
android:layout_marginTop="26dp"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etName"
android:layout_above="@+id/textView2"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etMobileNumber"
android:layout_above="@+id/textView3"
android:layout_alignEnd="@+id/etName"
android:layout_toEndOf="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmail"
android:layout_alignBottom="@+id/textView3"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/textView2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/btnSave"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
这里是DataBaseHelper.java
package com.example.sambal.contactmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="contacts.db";
private static final String TABLE_NAME="contacts";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MOBILE_NUMBER INTEGER,E-MAIL TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,int mobile_number,String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("NAME", name);
contentValues.put("MOBILE_NUMBER", mobile_number);
contentValues.put("E-MAIL", email);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
}
AddNewContact.java
package com.example.sambal.contactmanager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddNewContact extends AppCompatActivity {
EditText name,mobile_number,email;
Button save;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_contact);
databaseHelper=new DatabaseHelper(this);
name= (EditText) findViewById(R.id.etName);
mobile_number= (EditText) findViewById(R.id.etMobileNumber);
email= (EditText) findViewById(R.id.etEmail);
save= (Button) findViewById(R.id.btnSave);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean result = databaseHelper.insertData(name.getText().toString(),
Integer.parseInt(mobile_number.getText().toString()), email.getText().toString());
if (result)
Toast.makeText(AddNewContact.this, "Data Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(AddNewContact.this, "Data Not Inserted", Toast.LENGTH_SHORT).show();
}
});
}
}
这是MainActivity.java
package com.example.sambal.contactmanager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
Button addcontact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper=new DatabaseHelper(this);
addcontact= (Button) findViewById(R.id.btnAdd);
addcontact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,AddNewContact.class);
startActivity(intent);
}
});
}
}
logcat
03-30 23:11:27.650 28750-28750/com.example.sambal.contactmanager E/SQLiteLog: (1) near "TABLEcontacts": syntax error
03-30 23:11:27.673 28750-28750/com.example.sambal.contactmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sambal.contactmanager, PID: 28750
android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error (code 1): , while compiling: CREATE TABLEcontacts (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MOBILE_NUMBER INTEGER,E-MAIL TEXT)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.sambal.contactmanager.DatabaseHelper.onCreate(DatabaseHelper.java:25)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.sambal.contactmanager.DatabaseHelper.insertData(DatabaseHelper.java:40)
at com.example.sambal.contactmanager.AddNewContact$1.onClick(AddNewContact.java:33)
at android.view.View.performClick(View.java:4790)
at android.view.View$PerformClick.run(View.java:19883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
03-30 23:11:54.392 29707-29707/com.example.sambal.contactmanager E/SQLiteLog: (1) near "TABLEcontacts": syntax error
03-30 23:11:54.397 29707-29707/com.example.sambal.contactmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sambal.contactmanager, PID: 29707
android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error (code 1): , while compiling: CREATE TABLEcontacts (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MOBILE_NUMBER INTEGER,E-MAIL TEXT)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.sambal.contactmanager.DatabaseHelper.onCreate(DatabaseHelper.java:25)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.sambal.contactmanager.DatabaseHelper.insertData(DatabaseHelper.java:40)
at com.example.sambal.contactmanager.AddNewContact$1.onClick(AddNewContact.java:33)
at android.view.View.performClick(View.java:4790)
at android.view.View$PerformClick.run(View.java:19883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
03-30 23:12:08.363 30145-30145/com.example.sambal.contactmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sambal.contactmanager, PID: 30145
java.lang.NumberFormatException: Invalid int: "fhh"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.sambal.contactmanager.AddNewContact$1.onClick(AddNewContact.java:34)
at android.view.View.performClick(View.java:4790)
at android.view.View$PerformClick.run(View.java:19883)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
答案 0 :(得分:2)
您需要在Table关键字和表名称之间添加一个空格:
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MOBILE_NUMBER INTEGER,E-MAIL TEXT)");
以下是一个例子:
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
db.execSQL(sql);
答案 1 :(得分:0)
语法是 &#34;创建表格(此处为空格)表格名称...&#34; 虽然看到你的logcat,看到查询,你在TABLE和CONTACTS之间缺少空格。
使用&#34;如果不存在则创建表&#34;