1.DbOpenHelper

package com.example.dbtest.dbHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; //继承SQLiteOpenHelper类
public class DbOpenHelper extends SQLiteOpenHelper { public DbOpenHelper(Context context) {
//Context 上下文
//name 数据库名
//CursorFactory 游标工厂模式 当为null时 使用默认值
//version 数据库版本 版本号从1开始
super(context, "test.db", null, 1);
} /**
* 第一次访问数据库 执行方法
* */
@Override
public void onCreate(SQLiteDatabase db) {
//创建表结构
String sql = "create table person(id integer primary key autoincrement,name varchar(20),age int,phone varchar(11))";
db.execSQL(sql); } @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

2.实体

package com.example.dbtest.domain;

public class Person {
private int id;
private String name;
private int age;
private String phone; public Person(){} public Person(int id, String name, int age, String phone) {
super();
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
} @Override
public String toString() {
return "个人信息[id=" + id + ", name=" + name + ", age=" + age
+ ", phone=" + phone + "]";
} }

3.personDao

package com.example.dbtest.dao;

import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import com.example.dbtest.dbHelper.DbOpenHelper;
import com.example.dbtest.domain.Person; public class PersonDao { private DbOpenHelper helper; public PersonDao(Context context)
{
helper = new DbOpenHelper(context);
} //添加一条记录
public void add(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "insert into person(name,age,phone) values(?,?,?)";
db.execSQL(sql, new Object[]{p.getName(),p.getAge(),p.getPhone()}); } //修改一条记录
public void update(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "update person set age=?,phone=? where name=?";
db.execSQL(sql, new Object[]{p.getAge(),p.getPhone(),p.getName()}); } //删除一条记录
public void delete(String name)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "delete from person where name=?";
db.execSQL(sql,new Object[]{name});
} public Person findByName(String name)
{ SQLiteDatabase db = helper.getReadableDatabase();
String sql = "select * from person where name=?";
Cursor cursor = db.rawQuery(sql, new String[]{name});
Person p = null;
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
p = new Person(id,name,age,phone);
}
return p;
} public List<Person> findAll(String name)
{ SQLiteDatabase db = helper.getReadableDatabase();
String sql = "select * from person where name=?";
Cursor cursor = db.rawQuery(sql, new String[]{name});
List<Person> list = new ArrayList();
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Person p = new Person(id,name,age,phone);
list.add(p);
}
return list;
} }

4.activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dbtest.MainActivity" > <Button
android:id="@+id/btn_createDB"
android:onClick="createDB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建数据库" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_createDB"
android:layout_below="@+id/btn_createDB"
android:text="姓名" /> <EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="21dp"
android:layout_marginTop="24dp"
android:ems="10"
/> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_createDB"
android:layout_below="@+id/et_name"
android:text="电话" /> <EditText
android:id="@+id/et_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="21dp"
android:layout_marginTop="24dp"
android:ems="10"
/> <Button
android:id="@+id/btn_save"
android:onClick="save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_phone"
android:text="保存" />
<Button
android:id="@+id/btn_update"
android:onClick="update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_save"
android:text="修改" />
<Button
android:id="@+id/btn_find"
android:onClick="findOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_update"
android:text="查询" />
<Button
android:id="@+id/btn_delete"
android:onClick="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_find"
android:text="删除" /> </RelativeLayout>

5.activity对应点击事件

package com.example.dbtest;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import com.example.dbtest.dao.PersonDao;
import com.example.dbtest.dbHelper.DbOpenHelper;
import com.example.dbtest.domain.Person; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void createDB(View v)
{
DbOpenHelper db = new DbOpenHelper(this);
//该句才真正创建数据库
db.getWritableDatabase();
Toast.makeText(this, "数据库创建成功", 0).show(); } public void save(View v)
{
EditText nameText = (EditText)this.findViewById(R.id.et_name);
EditText phoneText = (EditText)this.findViewById(R.id.et_phone);
String name = nameText.getText().toString();
String phone = phoneText.getText().toString();
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(phone))
{
Toast.makeText(this, "姓名和电话不能为空", 0).show();
return;
}
PersonDao dao = new PersonDao(this);
Person p = new Person();
p.setName(name);
p.setPhone(phone);
dao.add(p);
Toast.makeText(this, "保存成功", 0).show(); } public void update(View v)
{
EditText nameText = (EditText)this.findViewById(R.id.et_name);
EditText phoneText = (EditText)this.findViewById(R.id.et_phone);
String name = nameText.getText().toString();
String phone = phoneText.getText().toString(); PersonDao dao = new PersonDao(this);
Person p = new Person();
p.setName(name);
p.setPhone(phone);
dao.update(p);
Toast.makeText(this, "修改成功", 0).show(); } public void delete(View v)
{
EditText nameText = (EditText)this.findViewById(R.id.et_name);
EditText phoneText = (EditText)this.findViewById(R.id.et_phone);
String name = nameText.getText().toString();
String phone = phoneText.getText().toString(); PersonDao dao = new PersonDao(this);
dao.delete(name);
Toast.makeText(this, "删除成功", 0).show(); } }

android数据库简单操作的更多相关文章

  1. MongoDB数据库简单操作

    之前学过的有mysql数据库,现在我们学习一种非关系型数据库 一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数 ...

  2. MySQL数据库简单操作

    title date tags layout MySQL简单操作 2018-07-16 Linux post 登录mysql mysql -h 主机名 -u 用户名 -p 查看所有数据库 show d ...

  3. SQL数据库简单操作

    sql语言简介 (1)数据库是文件系统,使用标准sql对数据库进行操作 * 标准sql,在mysql里面使用语句,在oracle.db2都可以使用这个语句 (2)什么是sql * Structured ...

  4. Android基础之sqlite 数据库简单操作

    尽管很简单,但是也存下来,以后直接粘过去就能用了. public class DBHelper extends SQLiteOpenHelper {      private static final ...

  5. Oracle 数据库简单操作

    现在大型企业一般都用Oracle数据库,Oracle数据库在一般采用expdp,impdp 导出导入数据,但是在操作中经常会遇到一些问题.下面来浅析这些问题. 1. 导出数据 一般导出数据的时候需要建 ...

  6. 使用SQLiteOpenHelper类对数据库简单操作

    实现数据库基本操作       数据库创建的问题解决了,接下来就该使用数据库实现应用程序功能的时候了.基本的操作包括创建.读取.更新.删除,即我们通常说的CRUD(Create, Read, Upda ...

  7. Java 数据库简单操作类

    数据库操作类,将所有连接数据库的配置信息以及基本的CRUD操作封装在一个类里,方便项目里使用,将连接数据库的基本信息放在配置文件 "dbinfo.properties" 中,通过类 ...

  8. oracle数据库简单操作

    导入某用户所有表和数据:imp sgp/sgp@192.168.0.99:1521/orcl file=sgp20161025.dmp full=y 导出指定表及数据:exp sgp/sgp@192. ...

  9. SQL Server 的数据库简单操作

    --创建数据库--create database 数据库名称[on [primary](name='主数据逻辑文件名',filename='完整的路径.文件名和拓展名'[,size=文件大小][,fi ...

随机推荐

  1. 2016_NENU_CS_3

    贴一下比赛的代码,  其中 I 题代码源于final大神 ok_again http://acm.hust.edu.cn/vjudge/contest/127444#overview I /***** ...

  2. 云时代的IT运维面临将会有哪些变化

    导读 每一次IT系统的转型,运维系统和业务保障都是最艰难的部分.在当前企业IT系统向云架构转型的时刻,运维系统再一次面临着新的挑战.所以在数据中心运维的时候,运维人员应该注意哪些问题? 在云计算时代, ...

  3. wamp下var_dump()相关问题

    PHP 使用var_dump($arr)时 没有格式化输出. 原因是没有启用‘XDebug’扩展 [xdebug]zend_extension ="d:/wamp/bin/php/php7. ...

  4. c# 获取变量名

    也不知道哪里需要用到.反正很多人问. 这里就贴一下方法,也是忘记从哪里看到的了,反正是转载的! public static void Main(string[] args) { string abc= ...

  5. 洛谷P2054 [AHOI2005]洗牌(扩展欧几里德)

    洛谷题目传送门 来个正常的有证明的题解 我们不好来表示某时刻某一个位置是哪一张牌,但我们可以表示某时刻某一张牌在哪个位置. 设数列\(\{a_{i_j}\}\)表示\(i\)号牌经过\(j\)次洗牌后 ...

  6. 洛谷 P2059 [JLOI2013]卡牌游戏 解题报告

    P2059 [JLOI2013]卡牌游戏 题意 有\(n\)个人玩约瑟夫游戏,有\(m\)张卡,每张卡上有一个正整数,每次庄家有放回的抽一张卡,干掉从庄家起顺时针的第\(k\)个人(计算庄家),干掉的 ...

  7. 洛谷 P1627 [CQOI2009]中位数 解题报告

    P1627 [CQOI2009]中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式 ...

  8. 路径或文件名中含有中文的jar文件双击启动不了 -> Java7的Bug?

    至从安装了java7后,才发现部分可执行的jar文件双击都启动不了了. 比如所有的jar文件放在桌面上双击启动不了. 比如所有的文件名中含有中文的jar文件双击启动不了. 比如一个 abc.jar 放 ...

  9. Flash10 使用剪贴板得改变程序的写法了

    昨天一个客户告诉我,在她的电脑上无法复制图片的链接地址. 一开始,我以为是她操作有误,因为在我们的系统里使用的是一种“双保险”的复制方法. javascript + flash 两种方法来进行复制. ...

  10. R语言画棒状图(bar chart)和误差棒(error bar)

    假设我们现在有CC,CG,GG三种基因型及三种基因型对应的表型,我们现在想要画出不同的基因型对应表型的棒状图及误差棒.整个命令最重要的就是最后一句了,用arrows函数画误差棒.用到的R语言如下: d ...