0

您好我正在尝试创建一个数据库,然后根据从数据库返回的值设置文本视图值。我收到错误“SYNTAX ERROR(code1)”这是我的代码。

`package com.example.dbex;

import java.sql.SQLOutput;
import java.sql.SQLPermission;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="contactsmanager";
    public static final String TABLE_CONTACTS="contacts";
    public static final String KEY_ID="id";
    public static final String KEY_NAME="name";
    public static final String KEY_NUMBER="number";
    public DBHandler(Context context) {
        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_TABLE_CONTACTS="CREATE TABLE"+TABLE_CONTACTS+"("+KEY_ID+"INTEGER PRIMARY KEY,"+KEY_NAME+"TEXT,"+KEY_NUMBER+"TEXT"+")";
        db.execSQL(CREATE_TABLE_CONTACTS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS"+TABLE_CONTACTS);
        onCreate(db);
    }

    public void addContact(Contacts contacts)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(KEY_NAME, contacts.getname());
        values.put(KEY_NUMBER, contacts.getnumber());
        db.insert(TABLE_CONTACTS,null,values);
        db.close();
    }

    public Contacts getContact(int id)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_NAME,KEY_NUMBER},KEY_ID+"=?",new String[]{String.valueOf(id)}, null, null, null, null);
        if(c!=null)
        {
            c.moveToFirst();
        }
        Contacts contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
        return contacts;
    }

    public void deleteContact(Contacts contacts)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID+"=?", new String[]{String.valueOf(contacts.getID())});
        ``db.close();
    }


}
`

请帮我纠正我在这方面的错误。谢谢

4

2 回答 2

1

您的onUpgrade方法中的 SQL 中需要一个空格:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
    onCreate(db);
}

目前执行的SQL是: DROP TABLE IF EXISTScontacts

它需要是: DROP TABLE IF EXISTS contacts

这也适用于您的onCreate方法中的几个地方:

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_NUMBER+" TEXT"+")";
    db.execSQL(CREATE_TABLE_CONTACTS);
}

可以帮助我跟踪此类错误的方法是添加一个 try/catch 块并抛出您自己的异常,其中包括生成的 SQL,例如:

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_NUMBER+" TEXT"+")";
try {
        db.execSQL(CREATE_TABLE_CONTACTS);
} catch (SQLException sqlException) {
throw new SQLException("Error executing SQL: '" + CREATE_TABLE_CONTACTS + "', message: " + sqlException.getMessage(), sqlException);
}
    }

这使您可以查看错误消息以及导致它的 SQL。如果您这样做,建议您创建自己的execSQL包含此代码的方法。您甚至可以创建自己的包装器SQLiteDatabase并将此逻辑添加到您自己覆盖的execSQL.

于 2013-06-27T18:46:25.190 回答
0

在“create table”和“drop table if exists”之后需要一个空格:

"create table "
"drop table if exists "
于 2013-06-27T18:42:23.550 回答