Cursor 数据库查询游标的应用


然后运用moveToFirst,isAfterLast,moveToNext等方法查询


在Curesor中index是从0开始查询列数的

例子:
1.新建数据库SQliteOpenHelper类
package com.example.CursorDemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Anonymous on 2016/1/19.
*/
public class OpenHelpClass extends SQLiteOpenHelper {
public OpenHelpClass(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table stu (id integer primary key autoincrement,name text not null,age text not null)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists stu");

onCreate(db);
}
}

2.插入数据方法类:
package com.example.CursorDemo;

import android.database.sqlite.SQLiteDatabase;


public class MethodsClass {

private SQLiteDatabase db;

public MethodsClass(SQLiteDatabase db) {
this.db = db;
}

public void DataInset(String name,int age){

Object[] args = new Object[]{name, age};
db.execSQL("insert into stu(name,age) values (?,?)",args);
}

}

3.mainActivity类
package com.example.CursorDemo;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyActivity extends Activity implements View.OnClickListener{
private OpenHelpClass helper;
private SQLiteDatabase db;
private MethodsClass methods;
private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper = new OpenHelpClass(MyActivity.this, "stu.db", null, 1);
db = helper.getWritableDatabase();
createDatas();
btn.setOnClickListener(this);

}

//创建数据,插入数据
private void createDatas() {
btn = (Button) findViewById(R.id.check);
methods = new MethodsClass(db);
for (int i = 0; i < 20; i++) {

methods.DataInset("tom"+i,10+i);
}
db.close();
}

// 转到新一个activity显示结果
@Override
public void onClick(View v) {

startActivity(new Intent(MyActivity.this, ShowResult.class));

}
}

4.显示结果的activity
package com.example.CursorDemo;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class ShowResult extends Activity {

private OpenHelpClass helper;
private SQLiteDatabase db;
private List<String> list;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
helper = new OpenHelpClass(ShowResult.this, "stu.db", null, 1);
db = helper.getReadableDatabase();
GetDataFromDB();
showInListView(list);
}

// listView添加适配器
private void showInListView(List<String> list) {
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(new ArrayAdapter<String>(ShowResult.this, android.R.layout.simple_list_item_1, list));

}

//把数据库放到arrsylist中去
private void GetDataFromDB() {

list = new ArrayList<String>();
Cursor c = db.rawQuery("select id,name,age from stu", null);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){

list.add("" + c.getInt(0) + "" + " " + c.getString(1) + " " + c.getInt(2));

}
db.close();
}
}

结果:






posted @ 2016-02-28 17:45  奔跑吧_兄弟!  阅读(968)  评论(0编辑  收藏  举报