gpt4 book ai didi

安卓 : Sqlite error - (1) near "null": syntax error

转载 作者:行者123 更新时间:2023-11-30 02:32:52 32 4
gpt4 key购买 nike

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

我遇到了以下错误。

11-22 02:18:35.489: E/AndroidRuntime(2597): android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: 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();
}

这是我的联系人类

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");
}

}

我不知道我哪里做错了。如果我走错了路,请帮助。

最佳答案

如前所述,您需要为 ContactDB 类中的 public static String 赋值。您还应该将它们设为 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";

关于安卓 : Sqlite error - (1) near "null": syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27075232/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com