我一直在努力让listview在android studio中填充来自SQLite的数据。当用户单击按钮以查看酒店列表时,列表视图应该填充。但是,每次应用程序崩溃时,我都会收到相同的错误。
例外:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.aids.a09application / com.example.aids.a09application.HotelsList}: android.database.sqlite.SQLiteException:接近“to”:语法错误(代码 1):,编译时:INSERT INTO酒店 (hotel_id,hotel_name,电话,纬度,经度,价格,靠近)VALUES (1,'Curragh B& B','+ 35345456429',53.124905,-6.789059,'A','Mondello 园');
以下是SQLHelper类的代码:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_HOTEL = "CREATE TABLE IF NOT EXISTS " + TABLE_HOTEL + "("
+ HOTEL_ID + " INTEGER PRIMARY KEY NOT NULL,"
+ HOTEL_NAME + " TEXT,"
+ PHONE + " TEXT," + LATITUDE + " REAL,"
+ LONGITUDE + " REAL," + PRICE + " TEXT,"
+ NEAR_TO + "TEXT" +");";
db.execSQL(CREATE_HOTEL);
db.execSQL(insertHotel(1,"Curragh B&B", "+35345456429", 53.124905, -6.789059, "A", "Mondello Park"));
db.execSQL(insertHotel(2,"No. 5 Bed and Breakfast", "+353 1 708 6400", 53.130330, -6.755625, "A", "Mondello Park"));
db.execSQL(insertHotel(3,"Maynooth Campus", "+353 85 110 0907", 53.379794, -6.595520, "A", "Mondello Park"));
db.execSQL(insertHotel(4,"Tulloch","+353 45 531 988", 53.171297, -6.973274, "A", "Mondello Park"));
db.execSQL(insertHotel(5,"Kilcullen Home Stay","+353 87 650 7195", 53.127930, -6.746819, "A", "Mondello Park"));
db.execSQL(insertHotel(6,"Cherryville House B&B","+353 45 521 091", 53.155753,-6.963233, "A", "Mondello Park"));
db.execSQL(insertHotel(7,"Athgarvan B&B","+353 45 405 950", 53.153120, -6.779494, "A", "Mondello Park"));
db.execSQL(insertHotel(8,"Naas Court Hotel","+353 45 866 073", 53.216508, -6.664741, "A", "Mondello Park"));
db.execSQL(insertHotel(9,"Gardeners Cottage","+353 86 817 4354", 53.371688, -6.715289, "A", "Mondello Park"));
db.execSQL(insertHotel(10,"Bridge B&B","+353 45 881 891", 53.214835, -6.678269, "A", "Mondello Park"));
public String insertHotel(int hotelId, String hotelName, String phone, double latitude, double longitude, String price, String nearTo) {
String insertHotelToDB = "INSERT INTO " + TABLE_HOTEL
+ " (" + HOTEL_ID + "," + HOTEL_NAME + "," + PHONE + "," + LATITUDE + "," + LONGITUDE + "," + PRICE + "," + NEAR_TO
+ ") VALUES (" + hotelId + ",'" + hotelName + "','" + phone + "'," + latitude + "," + longitude + ",'" + price + "','" + nearTo + "')" + ";";
return insertHotelToDB;
}`
这是酒店列表类:
public class HotelsList extends AppCompatActivity {
ListView hotelList;
//SQLHelper db = new SQLHelper(this);
List<Hotel> getAllHotels;
ArrayAdapter adapter;
SQLHelper db = new SQLHelper(this);
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_list);
hotelList = (ListView)findViewById(R.id.hotel_listView);
getAllHotels = db.getAllHotels();
db.close();
// set up ArrayList for buildingNames
final List<String> hotelNames = new ArrayList<>();
// loop through getAllBuilding List and add all building_name 's to buildingNames list
for (int i = 0; i < getAllHotels.size(); i++) {
hotelNames.add(getAllHotels.get(i).getHotelName());
//sort alphabetically
Collections.sort(hotelNames);
}
// set up adapter for searchBar listView
adapter = new ArrayAdapter<>(HotelsList.this, android.R.layout.simple_list_item_1, hotelNames);
hotelList.setAdapter(adapter);
}
}
这是包含打开酒店列表按钮的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_open_hotels, container, false);
Button openHotel = (Button) view.findViewById(R.id.hotelsButton);
openHotel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentOpenHotelPage = new Intent(getActivity(), HotelsList.class);
startActivity(intentOpenHotelPage);
}
});
return view;
}
答案 0 :(得分:1)
您应该添加 SPACE
&amp;移除 ;
<强>不强>
+ NEAR_TO + "TEXT" +");";
<强>不要强>
+ NEAR_TO + " TEXT" + ")";
然后卸载 App&amp; RUN 。
答案 1 :(得分:0)
在您的查询中执行此操作,
+ NEAR_TO + "TEXT" + ")";
您的代码+ NEAR_TO + "TEXT" +");";
- 错误
答案 2 :(得分:0)
我建议稍微不同地插入数据库以避免语法相关的问题。 SQLiteDatabase
类能够通过ContentValues
执行基本的CRUD操作。一个简单的插入示例如下:
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_1, "your col 1 value here");
values.put(COLUMN_2, "your col 2 value here");
values.put(COLUMN_3, "your col 3 value here");
...
database.insert(TABLE_1, null, values);
通过遵循这样的结构,您不再需要以字符串形式生成查询并执行它们。这将使您的生活更轻松,您将遵守一般的SQLite指南,并使您的代码更具可读性。
另外作为另一个提示/旁注:我注意到你基本上使用数据库类的onCreate()中的一些值初始化表。当我以前需要做类似的事情时,我创建了一个initTable()函数,其中我遵循与上述相同的模式。但是请注意,插入必须在方法中多次出现以完成插入多行,因此该函数最终会看起来像这样:
private void initPeople(SQLiteDatabase db){
ContentValues values = new ContentValues();
values.put(FIRST_NAME, "Foo");
values.put(LAST_NAME, "Bar");
db.insert(TABLE_PEOPLE, null, values);
values.put(FIRST_NAME, "John");
values.put(LAST_NAME, "Smith");
db.insert(TABLE_PEOPLE, null, values);
}