PersonDao1.java

package mm.shandong.com.testsqlsqllite.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList;
import java.util.List; import mm.shandong.com.testsqlsqllite.entity.Person;
import mm.shandong.com.testsqlsqllite.util.StudySQLiteOpenHelper; /**
* Created by buyadong on 2016/8/7.
*/
public class PersonDao1 {
Context context;
StudySQLiteOpenHelper studySQLiteOpenHelper; public PersonDao1(Context context) {
this.context = context;
studySQLiteOpenHelper = new StudySQLiteOpenHelper(context, "androidStudy.db", 7);
} public void addPerson(Person person) {
SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
String sql = "insert into person(_id,name,sex,age,code) values(null,?,?,?,?)";
db.execSQL(sql,new Object[]{person.getName(),
person.getSex(), person.getAge(), person.getCode()});
} public void deletePerson(Person person) {
SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
String sql = "delete from person where _id=?";
db.execSQL(sql, new Object[]{person.get_id()});
} public void updatePerson(Person person) {
SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
String sql = "update person set name=?,sex=?,age=?,code=? where _id=?";
db.execSQL(sql, new Object[]{person.getName(), person.getSex(),
person.getAge(), person.getCode(), person.get_id()});
} public List<Person> getAllPerson() {
List<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
String sql = "select * from person";
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
String code = cursor.getString(cursor.getColumnIndex("code"));
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
Person person = new Person(name, sex, age, code);
person.set_id(_id);
persons.add(person);
}
return persons;
}
}

   Person实体类

package mm.shandong.com.testsqlsqllite.entity;

import java.io.Serializable;

/**
* Created by 安卓无忧 on 2016/7/27.
*/
public class Person implements Serializable{
private String name;
private int age;
private String code;
private String sex; public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} private String first_letter;
private int _id; public void set_id(int _id) {
this._id = _id;
} public int get_id() { return _id;
} public void setFirst_letter(String first_letter) {
this.first_letter = first_letter;
} public String getFirst_letter() { return first_letter;
} public Person(){ }
public Person(String name, int age, String code){
this.name=name;
this.age=age;
this.code=code;
}
public Person(String name,String sex, int age, String code){
this.name=name;
this.age=age;
this.code=code;
this.sex=sex;
}
@Override
public String toString() {
return name +" "+age+" "+code;
} public void setAge(int age) {
this.age = age;
} public void setCode(String code) {
this.code = code;
} public void setName(String name) {
this.name = name;
} public int getAge() { return age;
} public String getCode() {
return code;
} public String getName() {
return name;
}
}

 StudySQLiteOpenHelper

package mm.shandong.com.testsqlsqllite.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; /**
* Created by 安卓无忧 on 2016/7/10.
*/
public class StudySQLiteOpenHelper extends SQLiteOpenHelper {
String dataBase_name;
Context context;
String create_TABLE_sql="create table person(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name varchar(30),age integer,sex varchar(30),code varchar(30))" ;
String delete_Sql="delete from person"; public StudySQLiteOpenHelper(Context context, String name, int verson){
super(context,name,null,verson);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(create_TABLE_sql);
} @Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { }
}

  activity

package mm.shandong.com.testsqlsqllite;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import mm.shandong.com.testsqlsqllite.dao.PersonDao1;
import mm.shandong.com.testsqlsqllite.entity.Person; public class TestSQLSqlLiteActivity extends AppCompatActivity {
ListView listView;
List<Person> persons;
PersonDao1 personDao;
BaseAdapter personAdapter;
Map<String,Boolean> radioStates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_sqlsql_lite);
radioStates=new HashMap<String,Boolean>();
personDao=new PersonDao1(this);
listView= (ListView) findViewById(R.id.listView);
persons=personDao.getAllPerson();
initListView();
}
public void initListView(){
personAdapter=new BaseAdapter() {
@Override
public int getCount() {
return persons.size();
}
@Override
public Object getItem(int position) {
return persons.get(position);
} @Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
view=getLayoutInflater().inflate(R.layout.item_sqlite_listview,null);
TextView textViewPerson_name= (TextView) view.findViewById(R.id.textViewPerson_name);
TextView textViewPerson_sex= (TextView) view.findViewById(R.id.textViewPerson_sex);
TextView textViewPerson_code=(TextView)view.findViewById(R.id.textViewPerson_code);
TextView textViewPerson_age= (TextView) view.findViewById(R.id.textViewPerson_age);
final Person person= (Person) getItem(position);
textViewPerson_name.setText(person.getName());
textViewPerson_age.setText(person.getAge()+"");
textViewPerson_sex.setText(person.getSex());
textViewPerson_code.setText(person.getCode());
RadioButton radioButton= (RadioButton) view.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view instanceof RadioButton) {
RadioButton radioButton = (RadioButton) view;
if (radioButton.isChecked()) {
radioStates.put(String.valueOf(position), true);
for (String key : radioStates.keySet()) {
if (!key.equals(String.valueOf(position))) {
radioStates.put(key, false);
}
}
notifyDataSetChanged();
}
} }
}
);
Boolean tempState=radioStates.get(String.valueOf(position));
if(tempState!=null&&tempState){
radioButton.setChecked(true);
view.setBackgroundColor(Color.BLUE);
}else{
radioButton.setChecked(false);
view.setBackgroundColor(Color.WHITE);
}
return view;
}
};
listView.setAdapter(personAdapter);
}
public void addPerson(View view){
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("新增用户").create();
View childView=getLayoutInflater().inflate(R.layout.layout_sqlite_alert,null);
builder.setView(childView);
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Field field = null;
try {
field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialogInterface, true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { try {
Field field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialogInterface,false);
AlertDialog ad;
if(dialogInterface instanceof AlertDialog){
ad= (AlertDialog) dialogInterface;
EditText editViewPerson_name=
(EditText) ad.findViewById(R.id.editViewPerson_name);
EditText editViewPerson_code=
(EditText) ad.findViewById(R.id.editViewPerson_code);
EditText editViewPerson_age=
(EditText) ad.findViewById(R.id.editViewPerson_age);
RadioGroup radioGroupSex=
(RadioGroup) ad.findViewById(R.id.radioGroupSex);
if(TextUtils.isEmpty(editViewPerson_name.getText().toString())){
Toast.makeText(TestSQLSqlLiteActivity.this,
"姓名不能为空",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(editViewPerson_code.getText().toString())){
Toast.makeText(TestSQLSqlLiteActivity.this,
"编号不能为空",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(editViewPerson_age.getText().toString())){
Toast.makeText(TestSQLSqlLiteActivity.this,
"年龄不能为空",Toast.LENGTH_SHORT).show();
return;
}
Person person=new Person();
person.setAge(Integer.parseInt(editViewPerson_age.getText().toString()));
person.setName(editViewPerson_name.getText().toString());
person.setCode(editViewPerson_code.getText().toString());
RadioButton radioButton= (RadioButton) radioGroupSex.
findViewById(radioGroupSex.getCheckedRadioButtonId());
person.setSex(radioButton.getText().toString());
personDao.addPerson(person);
persons=personDao.getAllPerson();
radioStates=new HashMap<String,Boolean>();
personAdapter.notifyDataSetChanged();
field.set(dialogInterface,true);
} } catch (Exception e) {
e.printStackTrace();
}
}
});
builder.show(); }
public void deletePerson(View view){
int position=-1;
for(int i=0;i<listView.getChildCount();i++){
View childView=listView.getChildAt(i);
RadioButton radioButton= (RadioButton) childView.findViewById(R.id.radioButton);
if(radioButton.isChecked()){
position=i;
break;
}
}
if(position!=-1){
Person person= persons.get(position);
personDao.deletePerson(person);
persons=personDao.getAllPerson();
radioStates=new HashMap<String,Boolean>();
personAdapter.notifyDataSetChanged(); }else{
Toast.makeText(this,"请选择要删除的行",Toast.LENGTH_SHORT);
}
}
public void updatePerson(View view){
int position=-1;
for(int i=0;i<listView.getChildCount();i++){
View childView=listView.getChildAt(i);
RadioButton radioButton= (RadioButton) childView.findViewById(R.id.radioButton);
if(radioButton.isChecked()){
position=i;
break;
}
}
if(position!=-1){
final Person person= persons.get(position);
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("修改用户").create();
View childView=getLayoutInflater().inflate(R.layout.layout_sqlite_alert,null);
EditText editViewPerson_name=
(EditText) childView.findViewById(R.id.editViewPerson_name);
EditText editViewPerson_code=
(EditText) childView.findViewById(R.id.editViewPerson_code);
EditText editViewPerson_age=
(EditText) childView.findViewById(R.id.editViewPerson_age);
RadioGroup radioGroupSex=
(RadioGroup) childView.findViewById(R.id.radioGroupSex);
editViewPerson_name.setText(person.getName());
editViewPerson_age.setText(String.valueOf(person.getAge()));
editViewPerson_code.setText(person.getCode());
if(person.getSex().equals("男")){
((RadioButton)radioGroupSex.getChildAt(0)).setChecked(true);
}else{
((RadioButton)radioGroupSex.getChildAt(1)).setChecked(true);
}
builder.setView(childView);
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { }
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { try {
Field field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(dialogInterface,false);
AlertDialog ad;
if(dialogInterface instanceof AlertDialog){
ad= (AlertDialog) dialogInterface;
EditText editViewPerson_name=
(EditText) ad.findViewById(R.id.editViewPerson_name);
EditText editViewPerson_age=
(EditText) ad.findViewById(R.id.editViewPerson_age);
EditText editViewPerson_code=
(EditText) ad.findViewById(R.id.editViewPerson_code);
RadioGroup radioGroupSex= (RadioGroup) ad.findViewById(R.id.radioGroupSex); if(TextUtils.isEmpty(editViewPerson_name.getText().toString())){
Toast.makeText(TestSQLSqlLiteActivity.this,
"姓名不能为空",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(editViewPerson_age.getText().toString())){
Toast.makeText(TestSQLSqlLiteActivity.this,
"年龄不能为空",Toast.LENGTH_SHORT).show();
return;
} person.setAge(Integer.parseInt(editViewPerson_age.getText().toString()));
person.setName(editViewPerson_name.getText().toString());
RadioButton radioButton= (RadioButton) radioGroupSex.
findViewById(radioGroupSex.getCheckedRadioButtonId());
person.setSex(radioButton.getText().toString());
person.setCode(editViewPerson_code.getText().toString());
personDao.updatePerson(person);
persons=personDao.getAllPerson();
radioStates=new HashMap<String,Boolean>();
personAdapter.notifyDataSetChanged();
field.set(dialogInterface,true);
} } catch (Exception e) {
e.printStackTrace();
}
}
});
builder.show(); }else{
Toast.makeText(this,"请选择要删除的行",Toast.LENGTH_SHORT);
}
} }

  布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"> <LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addPerson"
android:text="新增" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="deletePerson"
android:text="删除" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="updatePerson"
android:text="修改" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="姓名" /> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="编号" /> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="年龄" /> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="性别" /> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="请选择" />
</LinearLayout> <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>

  

如果想要源码,请留下邮箱。

Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy,源码例子文档一网打尽

sqlite 增删改查的更多相关文章

  1. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  2. iOS sqlite 增删改查 简单封装(基于 FMDB)

    /** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...

  3. C#Sqlite增删改查

    说到使用数据库的话,无非也就是对数据的增加,删除和修改以及查询.前文已经 创建好了程序,现在我们就可以来具体实现Sqlite的数据操作,增删改查. 第一步,创建连接字符串来连接数据库: private ...

  4. iOS SQLite 增删改查的封装(关系型)

    在工程里导入libsqlite3.tbd库(Xcode 7) #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...

  5. C# 使用 Dapper 实现 SQLite 增删改查

    Dapper 是一款非常不错的轻型 ORM 框架,使用起来非常方便,经常使用 EF 框架的人几乎感觉不到差别,下面是自己写的 Sqlite 通用帮助类: 数据连接类: public class SQL ...

  6. 回家前的挣扎——SQLite增删改查

    引言 最后一天,公司就两个人,也不知道弄点什么,就在网上找了Sqlite的文档,看了看,这里也是现学现卖,给自己找点事做,感觉时间过得还是比较快的,不然焦急等待,滋味不好受啊. SQLite简介 SQ ...

  7. iOS SQLite增删改查(简单应用)

    // 注意: 在工程里导入libsqlite3.tbd库(Xcode7,如果Xcode7以下的版本则导入libsqlite3.dylib). #import <UIKit/UIKit.h> ...

  8. sqlite增删改查

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  9. Android Sqlite 增删改查

    模拟 查询所有数据,增加一条数据,修改某一条数据,删除某一条数据: SQLiteOpenHelper 帮助类的介绍: import android.content.Context; import an ...

随机推荐

  1. JAVA 设计模式 外观模式

    用途 外观模式 (Facade) 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式是一种结构型模式. 结构

  2. 【转载】MVC使用HandleErrorAttribute自定义异常

    本文导读:在ASP.NET MVC中,可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特 ...

  3. 存储过程返回布尔值以及C#相关处理

    前段时间有在数据库以及程序之间使用到布尔(bool,Boolean)值的问题. 比如在SQL中,你想判断记录是否存? 通常你会这样写: FROM [dbo].[SixSResponsiblePerso ...

  4. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  5. 有关微信开发中errorcode:-1 errmsg:system error 错误的一点原因

    如果你在微信开发中遇到{"errcode":-1,"errmsg":"system error,hints:[req_id:]"}这样的错误 ...

  6. C#再识委托

    从C#1到C#3逐步认识委托,由于C#4与C#5对委托改动并不大,故不作说明. 好久没看.NET了,一直在搞HybridAPP,都忘得差不多了,这也是自己从书中摘下笔迹,供日后翻阅. C# 1 1.什 ...

  7. MEF入门之不求甚解,但力求简单能讲明白(二)

    在上一篇文章中,我们已经学到了很基本的MEF概念和使用方法. 但我们导出的是一个object类型的实例,只能用来tostring,没有引用部件类库,也不能用里面的成员方法. 本篇,我们逐渐往简单的文件 ...

  8. Hewlett-Packard Enterprise 实习总结日记

    人生的第一份正式的实习工作时在HP,受益颇多.突然要离开了,还是非常舍不得的.在公司实习这半年多时间,写篇博客,对这期间的成长经历做一些总结.先说一个典故:我叫史蒂夫·乔布斯,我想找一些零件来做一台频 ...

  9. No suitable driver found for jdbc:mysql://localhost:3306/dmc

    1. 使用java jdbc直接连接数据库操作, 出现题中错误, 参考网上说法, 将mysql连接jar放入jre拓展底下 不再报错

  10. python读取文件的前几行

    文件内容rolling.txt: There's a fire starting in my heart 我怒火中烧 Reaching a fever pitch and it's bringing ...