数据库数据添加到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
- 数据库数据添加到TextView详解(limite参数如何使用)
- php添加数据到数据库
- 如何所ACCESS数据库的数据添加到SQL SERVER数据库中?
- AJAX例子(前端数据添加到数据库)
- 将数据库数据添加到treeview控件
- C#中添加数据到数据库
- 将excel的数据添加到数据库
- remind javaweb数据添加到数据库
- 从网络请求数据添加到数据库
- JDBC 批量添加数据到数据库
- 将json数据添加到数据库
- 使用JDBC添加带参数的数据
- 使用SqlBulkCopy类将海量数据添加到SQL数据库中
- 使用DataGridView添加数据源,同步到数据库
- Shiro使用RememberMe添加数据到Session
- 如何将备份数据还原到数据库
- 如何批量导入数据到Sqlite数据库
- iOS 演示 Modal Segue 的使用,包括参数如何从呈现者传递到 ModalView 以及数据如何从 ModalView 传回呈现者(利用委托对象)
- 推荐!手把手教你使用Git
- 感谢你给我机会上场
- 工具的使用 —— 键盘
- Storm如何保证消息不丢失
- 20多岁的你我
- 数据库数据添加到TextView详解(limite参数如何使用)
- Python与机器学习(四)决策树
- rsyslogd-学习&使用
- ios加速审核
- cordova+Ionic环境搭建
- 半年小总结
- 【杭电】[1702]ACboy needs your help again!
- 重新生成索引存储过程 sp_rebuild_index
- Linux一键安装web环境全攻略(阿里云服务器)