android SQLite数据库使用实例

创建数据库帮助类DbHelper.java:

1package com.example.db; 2 3import android.content.Context; 4import android.database.sqlite.SQLiteDatabase; 5import android.database.sqlite.SQLiteDatabase.CursorFactory; 6import android.database.sqlite.SQLiteOpenHelper; 7 8public class DbHelper extends SQLiteOpenHelper { 9 10 public DbHelper(Context context, String name, CursorFactory factory, 11 int version) { 12 super(context, name, factory, version); 13 // TODO Auto-generated constructor stub 14 } 15 16 @Override 17 public void onCreate(SQLiteDatabase db) { 18 19 db.execSQL("create table if not exists tb_people"+ 20 "(_id integer primary key autoincrement,"+ 21 "name varchar(20),"+ 22 "phone varchar(12),"+ 23 "mobile varchar(12),"+ 24 "email varchar(30))"); 25 } 26 27 @Override 28 public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 29 // TODO Auto-generated method stub 30 31 } 32 33}

主布局文件:

ch9_sqlmain.xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <TextView android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:text="所有联系人:" 9 android:textSize="15px"/> 10 <LinearLayout android:orientation="horizontal" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content"> 13 <TextView android:layout_width="40px" 14 android:layout_height="wrap_content" 15 android:text="编号"/> 16 <TextView android:layout_width="50px" 17 android:layout_height="wrap_content" 18 android:text="姓名"/> 19 <TextView android:layout_width="80px" 20 android:layout_height="wrap_content" 21 android:text="电话"/> 22 <TextView android:layout_width="80px" 23 android:layout_height="wrap_content" 24 android:text="手机"/> 25 <TextView android:layout_width="fill_parent" 26 android:layout_height="wrap_content" 27 android:text="电子信箱"/> 28 </LinearLayout> 29 <ListView android:layout_width="wrap_content" 30 android:layout_height="fill_parent" 31 android:id="@+id/list_people"/> 32 33</LinearLayout>

编写ListView的Item显示布局文件ch9_peoplelist.xml:

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" > 6 <TextView android:id="@+id/id" 7 android:layout_width="40px" 8 android:layout_height="wrap_content"/> 9 <TextView android:id="@+id/name" 10 android:layout_width="50px" 11 android:layout_height="wrap_content"/> 12 <TextView android:id="@+id/phone" 13 android:layout_width="80px" 14 android:layout_height="wrap_content"/> 15 <TextView android:id="@+id/mobile" 16 android:layout_width="80px" 17 android:layout_height="wrap_content"/> 18 <TextView android:id="@+id/email" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content"/> 21 22 23</LinearLayout>

主活动类SqlMainActivity.java:

1package com.example.ch9; 2 3import com.example.baseexample.R; 4import com.example.db.DbHelper; 5 6import android.app.Activity; 7import android.content.Intent; 8import android.database.Cursor; 9import android.database.sqlite.SQLiteDatabase; 10import android.os.Bundle; 11import android.view.ContextMenu; 12import android.view.ContextMenu.ContextMenuInfo; 13import android.view.Menu; 14import android.view.MenuItem; 15import android.view.View; 16import android.widget.AdapterView.AdapterContextMenuInfo; 17import android.widget.ListView; 18import android.widget.SimpleCursorAdapter; 19import android.widget.TextView; 20 21public class SqlMainActivity extends Activity { 22 private ListView list_people; 23 private DbHelper dbhelper; 24 private SQLiteDatabase db; 25 26 public void onCreate(Bundle savedInstanceState){ 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.ch9_sqlmain); 29 30 list_people = (ListView)findViewById(R.id.list_people); 31 32 dbhelper = new DbHelper(this, "Db_People", null, 1); 33 34 db = dbhelper.getReadableDatabase(); 35 Cursor c = db.query("tb_people", new String[]{"_id","name","phone","mobile","email"}, null, null, null, null, null); 36 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.ch9_peoplelist, c, new String[]{"_id","name","phone","mobile","email"}, new int[]{R.id.id,R.id.name,R.id.phone,R.id.mobile,R.id.email}); 37 38 this.list_people.setAdapter(adapter); 39 this.registerForContextMenu(list_people); 40 } 41 42 public boolean onCreateOptionsMenu(Menu menu){ 43 menu.add(Menu.NONE,Menu.FIRST+1,1,"添加").setIcon(android.R.drawable.ic_menu_add); 44 menu.add(Menu.NONE,Menu.FIRST+1,2,"退出").setIcon(android.R.drawable.ic_menu_delete); 45 return true; 46 } 47 48 public boolean onOptionsItemSelected(MenuItem item){ 49 switch(item.getItemId()){ 50 case Menu.FIRST+1: 51 Intent intent = new Intent(); 52 intent.setClass(SqlMainActivity.this, AddPeopleActivity.class); 53 startActivity(intent); 54 break; 55 case Menu.FIRST+2:finish(); 56 break; 57 } 58 return super.onOptionsItemSelected(item); 59 } 60 61 public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){ 62 menu.setHeaderIcon(R.drawable.ic_launcher); 63 menu.add(0,3,0,"修改"); 64 menu.add(0, 4, 0, "删除"); 65 } 66 67 public boolean onContextItemSelected(MenuItem item){ 68 AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo(); 69 switch(item.getItemId()){ 70 case 3: 71 String name=((TextView)menuInfo.targetView.findViewById(R.id.name)).getText().toString(); 72 String phone=((TextView)menuInfo.targetView.findViewById(R.id.phone)).getText().toString(); 73 String mobile=((TextView)menuInfo.targetView.findViewById(R.id.mobile)).getText().toString(); 74 String email=((TextView)menuInfo.targetView.findViewById(R.id.email)).getText().toString(); 75 Intent intent = new Intent(); 76 intent.setClass(SqlMainActivity.this, AddPeopleActivity.class); 77 Bundle bundle = new Bundle(); 78 bundle.putLong("id", menuInfo.id); 79 bundle.putString("name", name); 80 bundle.putString("phone", phone); 81 bundle.putString("mobile", mobile); 82 bundle.putString("email", email); 83 intent.putExtras(bundle); 84 startActivity(intent); 85 break; 86 case 4: 87 dbhelper = new DbHelper(this,"Db_People",null,1); 88 db = dbhelper.getWritableDatabase(); 89 db.delete("tb_people", "_id=?", new String[]{menuInfo.id+""}); 90 break; 91 } 92 93 return true; 94 } 95 96}

布局文件ch9_addpeople.xml:

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <LinearLayout android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:orientation="horizontal"> 9 <TextView android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="用户名" 12 android:layout_weight="2"/> 13 <EditText android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:id="@+id/edt_name" 16 android:layout_weight="1"/> 17 </LinearLayout> 18 <LinearLayout android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:orientation="horizontal"> 21 <TextView android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 android:text="联系电话" 24 android:layout_weight="2"/> 25 <EditText android:layout_width="fill_parent" 26 android:layout_height="wrap_content" 27 android:id="@+id/edt_phone" 28 android:layout_weight="1"/> 29 </LinearLayout> 30 <LinearLayout android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:orientation="horizontal"> 33 <TextView android:layout_width="fill_parent" 34 android:layout_height="wrap_content" 35 android:text="手机" 36 android:layout_weight="2"/> 37 <EditText android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:id="@+id/edt_mobile" 40 android:layout_weight="1"/> 41 </LinearLayout> 42 <LinearLayout android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:orientation="horizontal"> 45 <TextView android:layout_width="fill_parent" 46 android:layout_height="wrap_content" 47 android:text="电子信箱" 48 android:layout_weight="2"/> 49 <EditText android:layout_width="fill_parent" 50 android:layout_height="wrap_content" 51 android:id="@+id/edt_email" 52 android:layout_weight="1"/> 53 </LinearLayout> 54 <LinearLayout android:layout_width="fill_parent" 55 android:layout_height="wrap_content" 56 android:orientation="horizontal"> 57 <Button android:layout_width="fill_parent" 58 android:layout_height="wrap_content" 59 android:id="@+id/bt_save" 60 android:text="保存" 61 android:layout_weight="1"/> 62 <Button android:layout_width="fill_parent" 63 android:layout_height="wrap_content" 64 android:id="@+id/bt_cancel" 65 android:text="取消" 66 android:layout_weight="1"/> 67 </LinearLayout> 68 69</LinearLayout>

创建活动AddPeopleActivity.java:

1package com.example.ch9; 2 3import com.example.baseexample.R; 4import com.example.db.DbHelper; 5 6import android.app.Activity; 7import android.content.ContentValues; 8import android.database.sqlite.SQLiteDatabase; 9import android.os.Bundle; 10import android.view.View; 11import android.widget.Button; 12import android.widget.EditText; 13import android.widget.Toast; 14 15public class AddPeopleActivity extends Activity { 16 private EditText edt_name; 17 private EditText edt_phone; 18 private EditText edt_mobile; 19 private EditText edt_email; 20 private Button bt_save; 21 String name,phone,mobile,email; 22 DbHelper dbhelper; 23 SQLiteDatabase db; 24 Bundle bundle; 25 protected void onCreate(Bundle savedInstanceState){ 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.ch9_addpeople); 28 29 edt_name=(EditText)findViewById(R.id.edt_name); 30 edt_phone=(EditText)findViewById(R.id.edt_phone); 31 edt_mobile=(EditText)findViewById(R.id.edt_mobile); 32 edt_email=(EditText)findViewById(R.id.edt_email); 33 bt_save = (Button)findViewById(R.id.bt_save); 34 35 bundle = this.getIntent().getExtras(); 36 if(bundle!=null){ 37 edt_name.setText(bundle.getString("name")); 38 edt_phone.setText(bundle.getString("phone")); 39 edt_mobile.setText(bundle.getString("mobile")); 40 edt_email.setText(bundle.getString("email")); 41 } 42 43 bt_save.setOnClickListener(new Button.OnClickListener(){ 44 45 @Override 46 public void onClick(View v) { 47 name = edt_name.getText().toString(); 48 phone = edt_phone.getText().toString(); 49 mobile = edt_mobile.getText().toString(); 50 email = edt_email.getText().toString(); 51 52 ContentValues value = new ContentValues(); 53 value.put("name", name); 54 value.put("phone", phone); 55 value.put("mobile", mobile); 56 value.put("email", email); 57 58 DbHelper dbhelper = new DbHelper(AddPeopleActivity.this,"Db_People",null,1); 59 SQLiteDatabase db = dbhelper.getWritableDatabase(); 60 long status; 61 if(bundle!=null){ 62 status = db.update("tb_people", value, "_id=?", new String[]{bundle.getLong("id")+""}); 63 }else{ 64 status = db.insert("tb_people", null, value); 65 } 66 if(status!=-1){ 67 Toast.makeText(AddPeopleActivity.this, "保存成功", Toast.LENGTH_LONG).show(); 68 }else{ 69 Toast.makeText(AddPeopleActivity.this, "保存失败", Toast.LENGTH_LONG).show(); 70 } 71 } 72 73 }); 74 } 75}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )