我必须创建一个应用程序,为此我需要创建一个数据库。 我能够创建新条目,但我不知道如何读取数据库以获取存储的数据并将其放入ListView。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="students.db";
private static final String TABLE_NAME="student_details";
private static final String COL_1="ID";
private static final String COL_2="NAME";
private static final String COL_3="SURNAME";
private static final String COL_4="MARKS";
private static final int DATABASE_VERSION=1;
public DatabaseHelper (Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
Log.d("info", "TABLE WAS CREATED");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
onCreate(db);
Log.d("Info","TABLE WAS UPDATED");
}
public boolean insertdata(String name,String surname,String marks)
{
SQLiteDatabase db= this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3, surname);
contentValues.put(COL_4, marks);
long result=db.insert(TABLE_NAME,null,contentValues);
if (result==-1)
return false;
else
return true;
}
public Cursor getallData()
{
SQLiteDatabase db= this.getWritableDatabase();
Cursor res=db.rawQuery("SELECT * FROM "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String name,String surname,String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
// contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, surname);
contentValues.put(COL_4, marks);
db.update(TABLE_NAME, contentValues, COL_1 + " =? ", new String[]{id});
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from student_details where ID="+id+"", null );
if(res != null && res.moveToFirst()) {
return res;
}
return res;
}
public int deleteData(String id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, COL_1 + " =?", new String[]{id});
}
public class DisplayData extends Activity {
DatabaseHelper myDB;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_display);
list= (ListView) findViewById(R.id.listView);
myDB= new DatabaseHelper(this);
//Cursor res = myDB.getallData();
String[] from={"ID","NAME","SURNAME","MARKS"};
int[] to={R.id.textView11,R.id.textView13,R.id.textView15 };
Cursor cursor=myDB.getallData();
// allData();
SimpleCursorAdapter adapter= new SimpleCursorAdapter(this,R.layout.custom_data_display,cursor,from,to);
list.setAdapter(adapter);
// adapter= new ArrayAdapter(this,R.layout.custom_data_display,)
}
这是我得到的错误:
com.example.ky.studentmarks E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aeiltech.studentmarks, PID: 23469
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ky.studentmarks/com.example.aeiltech.studentmarks.DisplayData}: java.lang.IllegalArgumentException: column '_id' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
at android.app.ActivityThread.access$900(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在进入您不了解的编码之前,您应该先尝试一下教程。
官方:
1. schema。
类似:
2. http://developer.android.com/training/basics/data-storage/databases.html。
好教程:
3. Android column '_id' does not exist?。
您的问题似乎是您没有SimpleCursor在数据库中需要的那一列(" _id")。检查(2)您遇到的这个问题,并且为了解如何解决问题,请看(3)。
快乐的编码。
答案 1 :(得分:0)
将表格中的var allClasses = GetProjectItems(Context.ActiveProject.ProjectItems).Where(v => v.Name.Contains(".cs"));
// check for .cs extension on each
foreach (var c in allClasses)
{
var eles = c.FileCodeModel;
if (eles == null)
continue;
foreach (var ele in eles.CodeElements)
{
if (ele is EnvDTE.CodeNamespace)
{
var ns = ele as EnvDTE.CodeNamespace;
// run through classes
foreach (var property in ns.Members)
{
var member = property as CodeType;
if (member == null)
continue;
foreach (var d in member.Bases)
{
var dClass = d as CodeClass;
if (dClass == null)
continue;
var name = member.Name;
if (dClass.Name == "DataMigrationImpl")
{
yield return new Migration(member);
}
}
}
}
}
}
更改为id
。此外,您需要将_id
字段声明为SimpleCursorAdapter adapter
类的字段,而不是DisplayData
中的局部变量。
答案 2 :(得分:-1)
尝试像这样创建数据库:
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "(" + COL_1 +
" integer primary key autoincrement, " +
COL_2 + " text not null, " + COL_3 + " text not null, " + COL_4 + " integer);";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
同样在
public Cursor getallData()
中,请务必在res.moveToFirst()
之前加return res
。
看看voggela tutorial因为你需要一个DAO类来从数据库中访问数据而不直接使用它。