Android:Sqlite错误 - (1)near" null":语法错误

时间:2014-11-22 07:45:37

标签: android sqlite android-sqlite

我正在从服务器获取数据作为JSONObject并保存到我的表中。

我正面临以下错误。

  

11-22 02:18:35.489:E / AndroidRuntime(2597):android.database.sqlite.SQLiteException:near" null&#34 ;:语法错误(代码1):,同时编译:create table Conntact(null INTEGER PRIMARY KEY AUTOINCREMENT,null TEXT,null TEXT,null TEXT,null TEXT,null TEXT,null TEXT);

for(int i=0;i<contactdata.length();i++)
        {
            JSONObject cont = contactdata.getJSONObject(i);
            String con_id = getValue(cont, "id");
            System.out.println("contactid"+con_id);
            String first_name = getValue(cont, "first_name");
            String last_name = getValue(cont, "last_name");
            String short_name = getValue(cont, "short_name");
            String pid = getValue(cont, "pid");
            String cid = getValue(cont, "cid");
            String number = getValue(cont, "914424411295");
            Conntact con = new Conntact(getActivity());
            con.insertValues(con_id, short_name, first_name, last_name, number, pid, cid);
            Toast.makeText(ctx, "contact inside", Toast.LENGTH_SHORT).show();
        }

这是我的Conntact类

public class Conntact {

public Conntact() {

    // TODO Auto-generated constructor stub
}
static SQLiteDatabase db;
static ContactDB dedb;
public Conntact(Context context)
{
    dedb = new  ContactDB(context);
}
public static SQLiteDatabase open()
{
    return dedb.getWritableDatabase();
}
public void close()
{
    dedb.close();
}
public void insertValues(String con_id, String short_name,String first_name,String last_name,String pid,String cid,String number)
{
    db = open();
    ContentValues values = new ContentValues();
    values.put(ContactDB.con_id, con_id);
    values.put(ContactDB.short_name, short_name);
    values.put(ContactDB.first_name, first_name);
    values.put(ContactDB.last_name, last_name);
    values.put(ContactDB.pid, pid);
    values.put(ContactDB.cid, cid);
    values.put(ContactDB.number, number);
    db.insert(ContactDB.TABLE_NAME, null, values);
    this.close();
}

public ArrayList<String> getValues()
{
    ArrayList<String> names = new ArrayList<String>();
    db = open();
    String query = "select * from "+ContactDB.TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    if(cursor.moveToFirst())
    {
        do{
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.con_id)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.number)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.first_name)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.last_name)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.short_name)));;
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.pid)));;
            //names.add(cursor.get)
        }while(cursor.moveToNext());
    }
    return names;

}
public static  boolean isAvailable(String number)
{
    db = open();
    String query = "select * from"+ContactDB.TABLE_NAME+"where"+ContactDB.number+"="+number;
    Cursor c =db.rawQuery(query, null);
    if(c==null)
    {
        return false;
    }else
    return true;
}
}

这是我的ContactDB类

public class ContactDB extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "AA_DB_NAME";
public static final String TABLE_NAME = "Conntact";
public static final int VERSION = 1;
public ContactDB(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
    // TODO Auto-generated constructor stub
}

public static String con_id;
public static String short_name;
public static String first_name;
public static String last_name;
public static String pid;
public static String cid;
public static String number;
public static String CREATE_QUERY = "create table "+TABLE_NAME+" ("+con_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+short_name+" TEXT, "+first_name+" TEXT, "+last_name+" TEXT, "+pid+" TEXT, "+cid+" TEXT, "+number+" TEXT);";
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_QUERY);
}

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

}

我不知道我在哪里犯错误。如果我走错了,请帮忙。

2 个答案:

答案 0 :(得分:1)

如上所述,您需要为public static String类中的ContactDB指定值。你也应该让它们final,以防止它们被意外修改。

public static final String con_id = "con_id";
public static final String short_name = "short_name";
public static final String first_name = "first_name";
public static final String last_name = "last_name";
public static final String pid = "pid";
public static final String cid = "cid";
public static final String number = "number";   

答案 1 :(得分:0)

public static String CREATE_QUERY = "create table "+TABLE_NAME+" ("+con_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+short_name+" TEXT, "+first_name+" TEXT, "+last_name+" TEXT, "+pid+" TEXT, "+cid+" TEXT, "+number+" TEXT);";

你的con_id获得空值。您可以使用类似&#34;&#34;

的硬编码字符串