我是Android和SQLite数据库的新手。我一直在关注从数据库存储和检索数据的在线示例,但在阅读时出错。 下面的代码是用于从数据库写入和读取数据的简单代码。写入时,应用程序中没有错误,但在读取数据时,应用程序会因显示错误而关闭。我正在使用Android studio 1.2.2并在Android 5.0.1 Galaxy S4上进行测试。任何人都可以告诉我这里有什么问题吗?
/*********************activity_main.xml *************/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Hello Hello"
android:id="@+id/msg"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ListView android:id="@+id/contactListView"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="@+id/msg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="48dp" />
</LinearLayout>
/*********************row.xml **************/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="10dip" >
<TextView android:id="@+id/Name"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_marginLeft="25dp" />
</LinearLayout>
/ ******************主要活动***************** /
public class MainActivity extends ActionBarActivity {
List<String> stringList = new ArrayList<String>();
ListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.contactListView);
adapter = new ListAdapter(this, stringList);
listview.setAdapter(adapter);
NameDatabase db = null;
db = new NameDatabase(this);
db.addContact(1, "Sujan Raj Shrestha");
db.addContact(2, "Kim Sae Hyun");
db.addContact(3, "Kim Jong Kwon");
Cursor cr = db.getData(0);
adapter.add(cr.getString(1).toString());
}
public class ListAdapter extends ArrayAdapter<String>{
public ListAdapter(Context context, List<String> object){
super(context, R.layout.row, object);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
String nm = stringList.get(position);
if (row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.row, null);
}
((TextView) row.findViewById(R.id.Name)).setText(nm);
return row;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
/ *************** NameDatabase.java **************** /
public class NameDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String NAMELIST = "nametable";
public NameDatabase (Context context){
super(context, NAMELIST, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
String CREATE_CONTACTS_TABLE = "CREATE TABLE contacts" +" (id INTEGER PRIMARY KEY, name TEXT NOT NULL )";
db.execSQL(CREATE_CONTACTS_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("Drop TABLE IF EXISTS" + NAMELIST);
onCreate(db);
}
public boolean addContact(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", id);
values.put("name", name);
db.insert(NAMELIST, null, values);
db.close();
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from nametable where id=" + id + "", null);
return res;
}
}
答案 0 :(得分:1)
我刚刚发现你的问题主要问题是你查询的表是不正确的,确实是你的数据库名称,正确版本 getData()如下: / p>
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts where id=" + id + "", null);
return res;
}
答案 1 :(得分:0)
尝试此操作以从数据库中获取数据
cur = db.query(true, "contacts", new String[] {
"id", "name"
}, "id = " + id, null, null, null, null, null);
答案 2 :(得分:0)
您正尝试使用数据库名称访问表的内容。尝试使用表名作为“联系人”。替换NAMELIST =“contacts”,也在构造函数中传递数据库名称 喜欢 super(context,DATABASE_NAME,null,DATABASE_VERSION); 并添加变量
字符串DATABASE_NAME =“someName”;
获取光标结果添加
res.moveToNext();
以下 Cursor res = db.rawQuery(“select * from nametable where id =”+ id +“”,null);
由于您正在查询表格,但默认情况下光标仍保持在-1位置。因此,我们必须将光标移动到第1行,依此类推。
希望这有效。 :) 和平五