0

这是我的 logcat 错误

08-20 10:37:52.463: E/Database(1361): Failure 1 (near "CREATE_TABLEfriends": syntax error) on 0x9f418b8 when preparing 'CREATE_TABLEfriends(id INTEGER PRIMARY KEY AUTOINCREMAENT,fname text(20), lname text(30), nickanme text(20))'.
08-20 10:37:52.481: D/AndroidRuntime(1361): Shutting down VM
08-20 10:37:52.481: W/dalvikvm(1361): threadid=1: thread exiting with uncaught exception (group=0xb60084f0)
08-20 10:37:52.527: E/AndroidRuntime(1361): FATAL EXCEPTION: main
08-20 10:37:52.527: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite/com.example.sqlite.insertActivity}: android.database.sqlite.SQLiteException: near "CREATE_TABLEfriends": syntax error: CREATE_TABLEfriends(id INTEGER PRIMARY KEY AUTOINCREMAENT,fname text(20), lname text(30), nickanme text(20))
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.os.Looper.loop(Looper.java:130)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invoke(Method.java:507)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at dalvik.system.NativeStart.main(Native Method)
08-20 10:37:52.527: E/AndroidRuntime(1361): Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLEfriends": syntax error: CREATE_TABLEfriends(id INTEGER PRIMARY KEY AUTOINCREMAENT,fname text(20), lname text(30), nickanme text(20))
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at com.example.sqlite.db.FriendsDBHelper.onCreate(FriendsDBHelper.java:30)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at com.example.sqlite.db.FriendsDB.<init>(FriendsDB.java:19)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at com.example.sqlite.insertActivity.onCreate(insertActivity.java:43)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-20 10:37:52.527: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-20 10:37:52.527: E/AndroidRuntime(1361):     ... 11 more

这是我的代码形式 FriendDBHelper

package com.example.sqlite.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FriendsDBHelper extends SQLiteOpenHelper {
 public static String TABLE_NAME ="friends";
 public static int TABLE_VERSION = 1;
 public static String TABLE_KEY_ID = "id";
 public static String TABLE_KEY_FNAME = "fname";
 public static String TABLE_KEY_LNAME = "lname";
 public static String TABLE_KEY_NICKNAME = "nickanme";
 public static String TABLE_CERATE_SQL = "CREATE_TABLE"+TABLE_NAME+"("+"" +
                                            ""+TABLE_KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMAENT,"+
                                            ""+TABLE_KEY_FNAME+" text(20), "+
                                            ""+TABLE_KEY_LNAME+" text(30), "+
                                            ""+TABLE_KEY_NICKNAME+" text(20)"+
                                        ")";

public FriendsDBHelper(Context context) {
    super(context, TABLE_NAME, null, TABLE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CERATE_SQL);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE"+TABLE_NAME);
    onCreate(db);
    Log.i("friend DB Helper","TABLE UPGRADED FROM "+oldVersion+"to"+newVersion);
}

}

当我将数据插入到 SQLite 时的这段代码

package com.example.sqlite;

import java.security.PublicKey;

import javax.xml.datatype.Duration;

import com.example.sqlite.db.FriendsDB;
import com.example.sqlite.db.FriendsDBHelper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class insertActivity extends Activity {
 private Context context;
 private FriendsDB db;
 EditText inputfname;
 EditText inputlname;
 EditText inputnickname;
 Button save;
 Button cancle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.insert_layout);

    //view matching
    inputfname = (EditText)findViewById(R.id.fname);
    inputlname = (EditText)findViewById(R.id.lname);
    inputnickname = (EditText)findViewById(R.id.nickname);
    save = (Button)findViewById(R.id.save);
    cancle = (Button)findViewById(R.id.cancle);

    //context
            context = this;
            db = new FriendsDB(context);

    //button event
    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent insertData = new Intent(insertActivity.this,FriendsListActivity.class);
            insertData.putExtra("fname", inputfname.getText().toString());
            insertData.putExtra("lname", inputlname.getText().toString());
            insertData.putExtra("nickname", inputnickname.getText().toString());
            if(insertData!=null){
                //insert data
                long recordID = db.insert(inputfname.getText().toString(), inputlname.getText().toString(), inputnickname.getText().toString());
                startActivity(insertData);
            }
        }
    } );

如果您想了解更多信息,请告诉我。谢谢

4

2 回答 2

1

我认为Syntax error您的 create table 语句中有一个,删除 " _" 并在space后面加上一个TABLE,然后更改AUTOINCREMAENTAUTOINCREMENT

public static String TABLE_CERATE_SQL = "CREATE TABLE "+TABLE_NAME+" ("+"" +
                                            ""+TABLE_KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+
                                            ""+TABLE_KEY_FNAME+" text(20), "+
                                            ""+TABLE_KEY_LNAME+" text(30), "+
                                            ""+TABLE_KEY_NICKNAME+" text(20)"+
                                        ")";
于 2013-08-20T04:35:25.353 回答
0

创建表时出现语法错误。应该是这样的

public static String TABLE_CERATE_SQL = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"("+"" +
                                            ""+TABLE_KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMAENT,"+
                                            ""+TABLE_KEY_FNAME+" text(20), "+
                                            ""+TABLE_KEY_LNAME+" text(30), "+
                                            ""+TABLE_KEY_NICKNAME+" text(20)"+
                                        ")";
于 2013-08-20T04:32:12.857 回答