gpt4 book ai didi

java - SQLite Android 运行时错误

转载 作者:行者123 更新时间:2023-12-01 13:48:01 26 4
gpt4 key购买 nike

我正在尝试关注this关于让 SQLite 数据库与 Android 应用程序一起使用的教程(第 3 部分)。我对内容做了一些更改,但代码应该是相同的。 Android 应用程序在打开后崩溃,没有显示任何内容,这是 LogCat 的输出:

11-25 11:42:43.281: E/AndroidRuntime(1098): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.munroproject/com.example.munroproject.MainActivity}: android.database.sqlite.SQLiteException: near "drop": syntax error (code 1): , while compiling: CREATE TABLE munro (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, area TEXT, height TEXT, grid TEXT, drop TEXT, feature TEXT, country TEXT, geographurl TEXT, latitude TEXT, longitude TEXT)

这是数据库助手的内容:

package com.example.munroproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "munro_directory";

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

public void onCreate(SQLiteDatabase db){
String sql = "CREATE TABLE munro (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"area TEXT, " +
"height TEXT, " +
"grid TEXT, " +
"drop TEXT, " +
"feature TEXT, " +
"country TEXT, " +
"geographurl TEXT, " +
"latitude TEXT, " +
"longitude TEXT)";
db.execSQL(sql);
ContentValues values = new ContentValues();
String inputvalue = "Ben Chonzie,Loch Tay to Perth,931,NN773308,645,cairn/shelter,S,NN7732430857,56.453851,-3.992057");
String[] msplit = inputvalue.split(",");
int j=0;
values.put("name",msplit[j++]);
values.put("area",msplit[j++]);
values.put("height",msplit[j++]);
values.put("grid",msplit[j++]);
values.put("drop",msplit[j++]);
values.put("feature",msplit[j++]);
values.put("country",msplit[j++]);
values.put("geograph",msplit[j++]);
values.put("latitute",msplit[j++]);
values.put("longitude",msplit[j++]);
db.insert("munro", "name", values);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS munro");
onCreate(db);
}

}

mainActivity 文件如下所示:

package com.example.munroproject;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
protected ListView munroList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
munroList = (ListView) findViewById (R.id.list);
}

@SuppressWarnings("deprecation")
public void search(View view) {
// || is the concatenation operation in SQLite
cursor = db.rawQuery("SELECT _id, name, height, region FROM munro WHERE name || ' ' || height LIKE ?",
new String[]{"%" + searchText.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(this,R.layout.munro_list_item,cursor,new String[] {"name", "height", "region"},new int[] {R.id.name, R.id.height, R.id.region});
munroList.setAdapter(adapter);
}

有什么建议吗?

最佳答案

“drop”是 SQLite 中的保留关键字,用于删除(或删除)表。如果您确实想使用它,可以将其用双引号引起来。请参阅下面的示例

CREATE TABLE "[tablename]" ("drop" text)

关于java - SQLite Android 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198820/

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