我在这里实现了一个SQLite数据库。该数据库由类Contact的对象组成。但是我的应用程序一开始就崩溃了。这是Logcat和代码。
logcat的
FATAL EXCEPTION: main
Process: com.example.nirvan.sqliteexample, PID: 30065
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.example.nirvan.sqliteexample / com.example.nirvan.sqliteexample.MainActivity
}: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
while compiling: CREATE TABLEcontacts(idINTEGER KEYname TEXT)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2257)
at android.app.ActivityThread.access$800(ActivityThread.java: 139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1210)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 136)
at android.app.ActivityThread.main(ActivityThread.java: 5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
while compiling: CREATE TABLEcontacts(idINTEGER KEYname 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: 1672)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java: 1603)
at com.example.nirvan.sqliteexample.DatabaseHandler.onCreate(DatabaseHandler.java: 28)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java: 252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java: 164)
at com.example.nirvan.sqliteexample.DatabaseHandler.addContact(DatabaseHandler.java: 40)
at com.example.nirvan.sqliteexample.MainActivity.onCreate(MainActivity.java: 21)
at android.app.Activity.performCreate(Activity.java: 5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2257)
at android.app.ActivityThread.access$800(ActivityThread.java: 139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1210)
FATAL EXCEPTION: main
Process: com.example.nirvan.sqliteexample, PID: 32547
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.example.nirvan.sqliteexample / com.example.nirvan.sqliteexample.MainActivity
}: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
while compiling: CREATE TABLEcontacts(idINTEGER KEYname TEXT)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2257)
at android.app.ActivityThread.access$800(ActivityThread.java: 139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1210)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 136)
at android.app.ActivityThread.main(ActivityThread.java: 5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
while compiling: CREATE TABLEcontacts(idINTEGER KEYname 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)
MainActivity.java
package com.example.nirvan.sqliteexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
// Inserting Contacts
//Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(1, "one"));
db.addContact(new Contact(2, "two"));
db.addContact(new Contact(3, "three"));
TextView text=(TextView) findViewById(R.id.text);
Contact cn;
cn=db.getContact(2);
text.setText(cn.name);
}
}
Contact.java
public class Contact
{
public String name;
public int id;
Contact(int id, String name)
{
this.id=id;
this.name=name;
}
}
DatabaseHandler.java
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
{
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="contactsManager";
private static final String TABLE_CONTACTS="contacts";
private static final String KEY_ID="id";
private static final String KEY_NAME="name";
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);}
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_CONTACTS_TABLE="CREATE TABLE"+TABLE_CONTACTS+"("+KEY_ID+"INTEGER PRIMARY KEY"+KEY_NAME+" TEXT"+")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS"+TABLE_CONTACTS);
onCreate(db);
}
void addContact(Contact contact)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.name);
values.put(KEY_ID, contact.id);
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
//2nd argument is String containing nullColumnHack
db.close(); // Closing database connection
}
Contact getContact(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
// return contact
return contact;
}
public int updateContact(Contact contact)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.name);
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.id) });
}
}
activityMain.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.nirvan.sqliteexample.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
答案 0 :(得分:1)
您在数据库中执行的查询是一个简单的错误。为关键字添加一些空格,不要混淆Sqlite,如下所示
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT " + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
答案 1 :(得分:1)
请参阅this,了解如何正确创建数据库。在&#34; CREATE TABLE&#34;之后,您将缺少空格。和TABLE_CONTACTS
答案 2 :(得分:1)
试试这个,
您在CREATE TABLE语句后错过了空间,我认为在PRIMARY KEY之后逗号(,)
String CREATE_CONTACTS_TABLE="CREATE TABLE "+ TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT"+")";