Android中的sqlite个人的理解
2016-06-10 23:14
309 查看
以下是工程的目录结构:
以下是activity_main.xml中的代码
<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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="添加数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/query"
android:text="查询数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="更新数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:text="删除数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteDB"
android:text="删除数据库" />
<ListView
android:background="#cdcdcd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
></ListView>
</LinearLayout>
以下是adapter_item.xml中的代码
<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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="添加数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/query"
android:text="查询数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="更新数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:text="删除数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteDB"
android:text="删除数据库" />
<ListView
android:background="#cdcdcd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
></ListView>
</LinearLayout>
以下是Student.java中的代码
public class Student {
private String stuName;
private int stuAge;
private String stuId;
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}
以下是DBHelper.java中的代码
/*
*SQLiteOpenHelper是一个抽象类
*/
public class DBHelper extends SQLiteOpenHelper{
//定义数据库的静态变量
private static String DATABASE_NAME="test1.db";
//定义数据库版本的静态变量
private static int ATABASE_VERSION = 1;
//在类初始化的时候创建数据库,也就是调用该构造方法的时候创建数据库
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, ATABASE_VERSION);
}
//数据库第一次被创建的时候调用onCreate()方法。
@Override
public void onCreate(SQLiteDatabase db) {
/**
* 执行如果不存在表就创建表,且只创建一次
*/
//db.execSQL("CREATE TABLE IF NOT EXISTS person(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER");
db.execSQL("CREATE TABLE IF NOT EXISTS student(_id INTEGER PRIMARY KEY AUTOINCREMENT,stuid VARCHAR,name VARCHAR, age INTEGER)");
}
/**
* 如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade()方法。
* 这个方法可以选择不写
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//创建成功,日志输出提示
Log.i("update a Database", "成功");
}
}
以下是MyBaseAdapter.java中的代码
public class MyBaseAdapter extends BaseAdapter{
private Context context;
private List<Student> students;
private LayoutInflater inflater;
private ViewHelder viewHelder;
public MyBaseAdapter(Context context,List<Student> students) {
this.context=context;
this.students=students;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return students.size();
}
@Override
public Object getItem(int position) {
return students.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHelder=new ViewHelder();
if(convertView==null){
convertView=inflater.inflate(R.layout.adapter_item,null);
viewHelder.name=(TextView) convertView.findViewById(R.id.name);
viewHelder.age=(TextView) convertView.findViewById(R.id.age);
viewHelder.stuid=(TextView) convertView.findViewById(R.id.stuid);
convertView.setTag(viewHelder);
}else{
convertView.getTag();
}
return convertView;
}
private class ViewHelder{
private TextView name;
private TextView age;
private TextView stuid;
}
}
以下是MainActivity,java中的代码
public class MainActivity extends Activity implements OnClickListener,OnItemLongClickListener{
private SQLiteDatabase db;
private Student stu;
private ContentValues cv;
private Cursor c;
private boolean exits=false;
private Button add,deleteDB,query,update,delete;
private List<Student> students;
private List<Student> stus;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 在这里定义数据库也行,在DBHelper中定义数据库也行,但一般都会选择在DBHelper类中定义数据库
*
*/
DBHelper db1=new DBHelper(MainActivity.this//,"test1.db",null,1
);
db=db1.getReadableDatabase();
students=new ArrayList<Student>();
initView();
stu=new Student();
cv=new ContentValues();
}
//初始化View
public void initView(){
add=(Button) findViewById(R.id.add);
deleteDB=(Button) findViewById(R.id.deleteDB);
query=(Button) findViewById(R.id.query);
update=(Button) findViewById(R.id.update);
delete=(Button) findViewById(R.id.delete);
listView =(ListView) findViewById(R.id.listView);
add.setOnClickListener(this);
deleteDB.setOnClickListener(this);
query.setOnClickListener(this);
update.setOnClickListener(this);
delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
/**
* 该方式可用来做注册功能!
* 或新闻中的历史的记录的功能
* 或添加生词表的功能
*/
stu.setStuId("000001");
stu.setStuAge(20);
stu.setStuName("fjx");
c=db.query(true, "student", null, null, null, null, null, null, null, null);
while(c.moveToNext())
{
String stuid=c.getString(c.getColumnIndex("stuid"));
System.out.println("stuid="+stuid);
if(stu.getStuId().equals(stuid)){
exits=true;
break;
}else{
exits=false;
}
}
if(exits){
toast(stu.getStuId()+"已存在!");
}else{
db.execSQL("INSERT INTO student VALUES(NULL,?,?,?)", new Object[]{stu.getStuId(),
stu.getStuName(),stu.getStuAge()});
students.add(stu);
}
break;
//第二插入数据的方式
/*Student stu1=new Student();
stu1.setStuId("000001");
stu1.setStuName("fjl");
stu1.setStuAge(24);
cv.put("stuid",stu1.getStuId() );
cv.put("name", stu1.getStuName());
cv.put("age", stu1.getStuAge());
db.insert("student", null, cv);*/
case R.id.query:
/**
* 查询:
* 可以按名称查询
* 可以按stuid查询
*
* 这里的例子我就以名称查询
*/
c=db.query(true, "student", null, null, null, null, null, null, null, null);
while(c.moveToNext()){
String name3=c.getString(c.getColumnIndex("name"));
if("fj1".equals(name3)){//这里定义一个用名称取查询的操作
String stuid=c.getString(c.getColumnIndex("stuid"));
String name=c.getString(c.getColumnIndex("name"));
int age=c.getInt(c.getColumnIndex("stuid"));
System.out.println("stuid="+stuid+" name="+name+" age="+age);
}
}
break;
case R.id.update:
toast("更新");
//更新数据。
/**
* 用来修改密码
* 或其他的个人的信息的操作在这里完成
*/
cv=new ContentValues();
cv.put("age",21);
db.update("student", cv, "name = ?", new String[]{"fjx"});
break;
case R.id.delete:
toast("删除");
deleteSuccess();
break;
case R.id.deleteDB:
deleteDatabase("test1.db");
break;
}
}
//执行删除操作后执行的代码
public void deleteSuccess(){
stus=new ArrayList<Student>();
c=db.query(true, "student", null, null, null, null, null, null, null, null);
while(c.moveToNext()){
Student stu3=new Student();
String stuid=c.getString(c.getColumnIndex("stuid"));
String name=c.getString(c.getColumnIndex("name"));
int age=c.getInt(c.getColumnIndex("stuid"));
stu3.setStuAge(age);
stu3.setStuName(name);
stu3.setStuId(stuid);
System.out.println("stuid="+stuid+" name="+name+" age="+age);
stus.add(stu3);
}
MyBaseAdapter adapter=new MyBaseAdapter(MainActivity.this, stus);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(this);
}
@Override
protected void onDestroy() {
db.close();
super.onDestroy();
}
public void toast(String text){
Toast.makeText(MainActivity.this, text, 1).show();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
db.delete("student", "name = ?", new String[]{stus.get(position).getStuName()});
deleteSuccess();//这行代码的作用是执行完删除操作后,会重新更新一下ListView
return false;
}
}
以下是activity_main.xml中的代码
<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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="添加数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/query"
android:text="查询数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="更新数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:text="删除数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteDB"
android:text="删除数据库" />
<ListView
android:background="#cdcdcd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
></ListView>
</LinearLayout>
以下是adapter_item.xml中的代码
<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"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="添加数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/query"
android:text="查询数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="更新数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:text="删除数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteDB"
android:text="删除数据库" />
<ListView
android:background="#cdcdcd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
></ListView>
</LinearLayout>
以下是Student.java中的代码
public class Student {
private String stuName;
private int stuAge;
private String stuId;
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}
以下是DBHelper.java中的代码
/*
*SQLiteOpenHelper是一个抽象类
*/
public class DBHelper extends SQLiteOpenHelper{
//定义数据库的静态变量
private static String DATABASE_NAME="test1.db";
//定义数据库版本的静态变量
private static int ATABASE_VERSION = 1;
//在类初始化的时候创建数据库,也就是调用该构造方法的时候创建数据库
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, ATABASE_VERSION);
}
//数据库第一次被创建的时候调用onCreate()方法。
@Override
public void onCreate(SQLiteDatabase db) {
/**
* 执行如果不存在表就创建表,且只创建一次
*/
//db.execSQL("CREATE TABLE IF NOT EXISTS person(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER");
db.execSQL("CREATE TABLE IF NOT EXISTS student(_id INTEGER PRIMARY KEY AUTOINCREMENT,stuid VARCHAR,name VARCHAR, age INTEGER)");
}
/**
* 如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade()方法。
* 这个方法可以选择不写
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//创建成功,日志输出提示
Log.i("update a Database", "成功");
}
}
以下是MyBaseAdapter.java中的代码
public class MyBaseAdapter extends BaseAdapter{
private Context context;
private List<Student> students;
private LayoutInflater inflater;
private ViewHelder viewHelder;
public MyBaseAdapter(Context context,List<Student> students) {
this.context=context;
this.students=students;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return students.size();
}
@Override
public Object getItem(int position) {
return students.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHelder=new ViewHelder();
if(convertView==null){
convertView=inflater.inflate(R.layout.adapter_item,null);
viewHelder.name=(TextView) convertView.findViewById(R.id.name);
viewHelder.age=(TextView) convertView.findViewById(R.id.age);
viewHelder.stuid=(TextView) convertView.findViewById(R.id.stuid);
convertView.setTag(viewHelder);
}else{
convertView.getTag();
}
return convertView;
}
private class ViewHelder{
private TextView name;
private TextView age;
private TextView stuid;
}
}
以下是MainActivity,java中的代码
public class MainActivity extends Activity implements OnClickListener,OnItemLongClickListener{
private SQLiteDatabase db;
private Student stu;
private ContentValues cv;
private Cursor c;
private boolean exits=false;
private Button add,deleteDB,query,update,delete;
private List<Student> students;
private List<Student> stus;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 在这里定义数据库也行,在DBHelper中定义数据库也行,但一般都会选择在DBHelper类中定义数据库
*
*/
DBHelper db1=new DBHelper(MainActivity.this//,"test1.db",null,1
);
db=db1.getReadableDatabase();
students=new ArrayList<Student>();
initView();
stu=new Student();
cv=new ContentValues();
}
//初始化View
public void initView(){
add=(Button) findViewById(R.id.add);
deleteDB=(Button) findViewById(R.id.deleteDB);
query=(Button) findViewById(R.id.query);
update=(Button) findViewById(R.id.update);
delete=(Button) findViewById(R.id.delete);
listView =(ListView) findViewById(R.id.listView);
add.setOnClickListener(this);
deleteDB.setOnClickListener(this);
query.setOnClickListener(this);
update.setOnClickListener(this);
delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
/**
* 该方式可用来做注册功能!
* 或新闻中的历史的记录的功能
* 或添加生词表的功能
*/
stu.setStuId("000001");
stu.setStuAge(20);
stu.setStuName("fjx");
c=db.query(true, "student", null, null, null, null, null, null, null, null);
while(c.moveToNext())
{
String stuid=c.getString(c.getColumnIndex("stuid"));
System.out.println("stuid="+stuid);
if(stu.getStuId().equals(stuid)){
exits=true;
break;
}else{
exits=false;
}
}
if(exits){
toast(stu.getStuId()+"已存在!");
}else{
db.execSQL("INSERT INTO student VALUES(NULL,?,?,?)", new Object[]{stu.getStuId(),
stu.getStuName(),stu.getStuAge()});
students.add(stu);
}
break;
//第二插入数据的方式
/*Student stu1=new Student();
stu1.setStuId("000001");
stu1.setStuName("fjl");
stu1.setStuAge(24);
cv.put("stuid",stu1.getStuId() );
cv.put("name", stu1.getStuName());
cv.put("age", stu1.getStuAge());
db.insert("student", null, cv);*/
case R.id.query:
/**
* 查询:
* 可以按名称查询
* 可以按stuid查询
*
* 这里的例子我就以名称查询
*/
c=db.query(true, "student", null, null, null, null, null, null, null, null);
while(c.moveToNext()){
String name3=c.getString(c.getColumnIndex("name"));
if("fj1".equals(name3)){//这里定义一个用名称取查询的操作
String stuid=c.getString(c.getColumnIndex("stuid"));
String name=c.getString(c.getColumnIndex("name"));
int age=c.getInt(c.getColumnIndex("stuid"));
System.out.println("stuid="+stuid+" name="+name+" age="+age);
}
}
break;
case R.id.update:
toast("更新");
//更新数据。
/**
* 用来修改密码
* 或其他的个人的信息的操作在这里完成
*/
cv=new ContentValues();
cv.put("age",21);
db.update("student", cv, "name = ?", new String[]{"fjx"});
break;
case R.id.delete:
toast("删除");
deleteSuccess();
break;
case R.id.deleteDB:
deleteDatabase("test1.db");
break;
}
}
//执行删除操作后执行的代码
public void deleteSuccess(){
stus=new ArrayList<Student>();
c=db.query(true, "student", null, null, null, null, null, null, null, null);
while(c.moveToNext()){
Student stu3=new Student();
String stuid=c.getString(c.getColumnIndex("stuid"));
String name=c.getString(c.getColumnIndex("name"));
int age=c.getInt(c.getColumnIndex("stuid"));
stu3.setStuAge(age);
stu3.setStuName(name);
stu3.setStuId(stuid);
System.out.println("stuid="+stuid+" name="+name+" age="+age);
stus.add(stu3);
}
MyBaseAdapter adapter=new MyBaseAdapter(MainActivity.this, stus);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(this);
}
@Override
protected void onDestroy() {
db.close();
super.onDestroy();
}
public void toast(String text){
Toast.makeText(MainActivity.this, text, 1).show();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
db.delete("student", "name = ?", new String[]{stus.get(position).getStuName()});
deleteSuccess();//这行代码的作用是执行完删除操作后,会重新更新一下ListView
return false;
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories