gpt4 book ai didi

android.database.sqlite.SQLiteException : near "SELECT ": syntax error (code 1): , 编译时:SELECT * FROM Table_Name

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:03 26 4
gpt4 key购买 nike

我得到 android.database.sqlite.SQLiteException: near "SELECT ": syntax error (code 1): , while compiling: SELECT * FROM universities Error.

我有以下类(class)

用户类为

public class User {
private int univ_id;

private String univ_name, univ_abbr;

public User(int univ_id, String univ_name, String univ_abbr) {

this.univ_id = univ_id;
this.univ_name = univ_name;
this.univ_abbr = univ_abbr;
}

public User(String univ_name, String univ_abbr) {

this.univ_name = univ_name;
this.univ_abbr = univ_abbr;
}

public User() {
// TODO Auto-generated constructor stub
}

public int getUniv_id() {
return univ_id;
}

public void setUniv_id(int univ_id) {
this.univ_id = univ_id;
}

public String getUniv_name() {
return univ_name;
}

public void setUniv_name(String univ_name) {
this.univ_name = univ_name;
}

public String getUniv_abbr() {
return univ_abbr;
}

public void setUniv_abbr(String univ_abbr) {
this.univ_abbr = univ_abbr;
}
}

DBStorageFirst 类作为

public class DBStorageFirst extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "STUPIDSID_OFFLINE_DB";

public static final String TABLE_NAME_UNIVERSITIES = "universities";


public static final String COLUMN_UNIV_ID = "univ_id";
public static final String COLUMN_UNIV_NAME = "univ_name";
public static final String COLUMN_UNIV_ABBR = "univ_abbr";

public DBStorageFirst(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table " + TABLE_NAME_UNIVERSITIES
+ "(" + COLUMN_UNIV_ID + " integer RIMARY KEY," + COLUMN_UNIV_NAME
+ " text," + COLUMN_UNIV_ABBR + " text )");
Log.e("Success", "Universities Table Created");

}

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

onCreate(db);
}

public void addUniversities(int univ_id, String univ_name, String univ_abbr) {
SQLiteDatabase db = this.getWritableDatabase();
String sql1 = "insert into " + DBStorageFirst.TABLE_NAME_UNIVERSITIES
+ " (" + DBStorageFirst.COLUMN_UNIV_ID + ","
+ DBStorageFirst.COLUMN_UNIV_NAME + ","
+ DBStorageFirst.COLUMN_UNIV_ABBR + ")" + " values(" + univ_id
+ ",'" + univ_name + " ','" + univ_abbr + "')";
db.execSQL(sql1);
// database.execSQL(sql2);
Log.e("Success", "Data inserted Successfully into Universities Table");
}

public List<User> getAllContacts() {
List<User> contactList = new ArrayList<User>();

String selectQuery = "SELECT * FROM "+TABLE_NAME_UNIVERSITIES;
SQLiteDatabase db1 = this.getWritableDatabase();
Cursor cursor = db1.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
User contact = new User();
contact.setUniv_id(Integer.parseInt(cursor.getString(0)));
contact.setUniv_name(cursor.getString(1));
contact.setUniv_abbr(cursor.getString(2));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
}

和 MainActivity 一样

DBStorageFirst db = new DBStorageFirst(this);

// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addUniversities(25, "ABC", "UA");
db.addUniversities(26, "DEF", "UB");
List<User> list = new ArrayList<User>();
list = db.getAllContacts();

for (User cn : list) {
String log = "Id: " + cn.getUniv_id() + " ,Name: "
+ cn.getUniv_name() + " ,Abbr: " + cn.getUniv_abbr();
// Writing Contacts to log
Log.d("Name: ", log);
}

最佳答案

看起来 SELECT 之后的空格不是普通空格 (0x20),而是一些其他字符,例如不间断空格 (0xa0)。将其替换为常规空格。

关于android.database.sqlite.SQLiteException : near "SELECT ": syntax error (code 1): , 编译时:SELECT * FROM Table_Name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25075695/

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