数据库数据添加到TextView详解(limite参数如何使用)

来源:互联网 发布:淘宝原单好店推荐豆瓣 编辑:程序博客网 时间:2024/04/29 20:16

一、创建数据库,创建表格

         1、创建开启数据库帮助器    ,直接在onCreate方法内,创建表格,以便主程序建立数据库时直接调用

            创建 MyOpenHelper类

public class MyOpenHelper extends SQLiteOpenHelper {    //Cursor 游标,何为游标,其是一个结果集的游标,封装查询数据库获得的数据    // String name 数据库的名称    //version 整性,版本号,方便更新数据库更新》=1    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        //查看API文档 ,不能点击上方的MyOpenHelper,因为它是新创建的,只能打开super文档        super(context, name, factory, version);    }    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table person(_id integer primary key autoincrement,name char(10),salary char(20),phone char(20))");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }}
    
    2、完成数据库的创建,获取其实例对象
      //创建数据库实例
        MyOpenHelper myOpenHelper=new MyOpenHelper(this,"people.db",null,1);
      //得到数据库对象
        SQLiteDatabase people=myOpenHelper.getWritableDatabase();
二、通过测试类,往数据库添加数据
此处略,
三、查询数据,得到的是Cursor对象,如何得到数据呢,这就要套用JAVABEAN 了
    在查询语句中有一个limite参数,此参数有两个整形参数,分别代表第几行开始,显示多少行..."10,20"表示从第10行开始,显示20行数据
    while(cursor.moveToNext()){    String name= cursor.getString(cursor.getColumnIndex("name"));    String salary= cursor.getString(cursor.getColumnIndex("salary"));    String phone=cursor.getString(cursor.getColumnIndex("phone"));
    //定义保存数据的类People    People people1=new People(name,salary,phone);
    将类添加进List队列    peopleList.add(people1);     }
四、取出数据
    
for(People people1:peopleList){
    //创建TextView组件    TextView textView=new TextView(MainActivity.this);    textView.setText(people1.toString());    ll.addView(textView);}
界面布局:
 
<?xml version="1.0" encoding="utf-8"?><ScrollView    android:layout_height="match_parent"    android:layout_width="match_parent"
<---------以下两行代码为根节点必有的代码---------->    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"><LinearLayout    android:id="@+id/ll"    xmlns:app="http://schemas.android.com/apk/res-auto" 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"    android:orientation="vertical"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:showIn="@layout/activity_main" tools:context=".MainActivity"></LinearLayout></ScrollView>
全代码:
 1、 MyOpenHelper类
    
public class MyOpenHelper extends SQLiteOpenHelper {    //Cursor 游标,何为游标,其是一个结果集的游标,封装查询数据库获得的数据    // String name 数据库的名称    //version 整性,版本号,方便更新数据库更新》=1    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        //查看API文档 ,不能点击上方的MyOpenHelper,因为它是新创建的,只能打开super文档        super(context, name, factory, version);    }    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table person(_id integer primary key autoincrement,name char(10),salary char(20),phone char(20))");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }}
  2、
public class People {    private  String name;    private  String salary;    private  String phone;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSalary() {        return salary;    }    public void setSalary(String salary) {        this.salary = salary;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    public People(String name, String salary, String phone) {        this.name = name;        this.salary = salary;        this.phone = phone;    }    @Override    public String toString() {        return " name=" + name  +                ", salary='" + salary  +                ", phone=" + phone;    }}
  3、 主类
    
public class MainActivity extends AppCompatActivity {    //数据库名称faminly    List<People> peopleList=null;    MyOpenHelper myOpenHelper=new MyOpenHelper(this,"people.db",null,1);    LinearLayout ll=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        SQLiteDatabase people=myOpenHelper.getWritableDatabase();        Cursor cursor = people.query("person", null, null, null, null, null, null);        peopleList=new ArrayList<People>();        ll= (LinearLayout) findViewById(R.id.ll);        while(cursor.moveToNext()){            String name= cursor.getString(cursor.getColumnIndex("name"));            String salary= cursor.getString(cursor.getColumnIndex("salary"));            String phone=cursor.getString(cursor.getColumnIndex("phone"));            People people1=new People(name,salary,phone);            peopleList.add(people1);        }        for(People people1:peopleList){            TextView textView=new TextView(MainActivity.this);            textView.setText(people1.toString());            ll.addView(textView);        }    }}
   

0 0
原创粉丝点击