码迷,mamicode.com
首页 > 移动开发 > 详细

android数据库简单操作

时间:2014-08-24 23:52:03      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   os   使用   io   

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 数据库名
        //factory 数据库工厂 当为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数据库简单操作

标签:android   style   blog   http   color   java   os   使用   io   

原文地址:http://www.cnblogs.com/zoro-zero/p/3933879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!