SQLite数据库的封装

一:建立实体类对象,字段名一定要和数据库里的一一对应
Book.java
[java]  view plain  copy
  1. <span style="font-size:14px;">package com.example.entity;  
  2.   
  3. public class Book {  
  4.     private Integer  id;  
  5.     private String  author;  
  6.     private double price;  
  7.     private Integer pages;  
  8.     private String name;  
  9.     public Integer getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(Integer id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getAuthor() {  
  16.         return author;  
  17.     }  
  18.     public void setAuthor(String author) {  
  19.         this.author = author;  
  20.     }  
  21.     public double getPrice() {  
  22.         return price;  
  23.     }  
  24.     public void setPrice(double price) {  
  25.         this.price = price;  
  26.     }  
  27.     public Integer getPages() {  
  28.         return pages;  
  29.     }  
  30.     public void setPages(Integer pages) {  
  31.         this.pages = pages;  
  32.     }  
  33.     public String getName() {  
  34.         return name;  
  35.     }  
  36.     public void setName(String name) {  
  37.         this.name = name;  
  38.     }  
  39.     public Book() {  
  40.         super();  
  41.         // TODO Auto-generated constructor stub  
  42.     }  
  43.     public Book(Integer id, String author, double price, Integer pages,  
  44.             String name) {  
  45.         super();  
  46.         this.id = id;  
  47.         this.author = author;  
  48.         this.price = price;  
  49.         this.pages = pages;  
  50.         this.name = name;  
  51.     }  
  52.     @Override  
  53.     public String toString() {  
  54.         return "Book [id=" + id + ", author=" + author + ", price=" + price  
  55.                 + ", pages=" + pages + ", name=" + name + "]";  
  56.     }     
  57.       
  58. }</span>  
Category.java
[cpp]  view plain  copy
  1. package com.example.entity;  
  2.   
  3. public class Category {  
  4.     private Integer  id;  
  5.     private String category_name;  
  6.     private Integer category_code;  
  7.     public Integer getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(Integer id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getCategory_name() {  
  14.         return category_name;  
  15.     }  
  16.     public void setCategory_name(String category_name) {  
  17.         this.category_name = category_name;  
  18.     }  
  19.     public Integer getCategory_code() {  
  20.         return category_code;  
  21.     }  
  22.     public void setCategory_code(Integer category_code) {  
  23.         this.category_code = category_code;  
  24.     }  
  25.     public Category() {  
  26.         super();  
  27.         // TODO Auto-generated constructor stub  
  28.     }  
  29.     public Category(Integer id, String category_name, Integer category_code) {  
  30.         super();  
  31.         this.id = id;  
  32.         this.category_name = category_name;  
  33.         this.category_code = category_code;  
  34.     }  
  35.       
  36. }  
二:在dao层建立数据库管理类
[java]  view plain  copy
  1. package com.example.sqllitehelp.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.example.entity.Book;  
  7. import com.example.sqllitehelp.MainActivity;  
  8. import com.example.sqllitehelp.MyDatabaseHelper;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.ContentValues;  
  12. import android.content.Context;  
  13. import android.database.Cursor;  
  14. import android.database.sqlite.SQLiteDatabase;  
  15. import android.os.Bundle;  
  16. import android.widget.Toast;  
  17.   
  18.   
  19. public class sqlitedao   {  
  20.     private MyDatabaseHelper myDatabaseHlper;  
  21.     public sqlitedao(Context context) {  
  22.         myDatabaseHlper=new MyDatabaseHelper(context, "BookStore.db"null1);  
  23.     }  
  24.     public void add_data(Book  bk ){  
  25.         SQLiteDatabase  db=myDatabaseHlper.getWritableDatabase();  
  26.         ContentValues values=new ContentValues();  
  27.         //开始组装第一条数据  
  28.         values.put("name", bk.getName());  
  29.         values.put("author", bk.getAuthor());  
  30.         values.put("pages",bk.getPages());  
  31.         values.put("price", bk.getPrice());  
  32.         db.insert("Book""name", values);  
  33.         //当values参数为空或者里面没有内容的时候,  
  34.         //我们insert是会失败的(底层数据库不允许插入一个空行),为了防止这种情况,我们要在这里指定一个列名,  
  35.         //到时候如果发现将要插入的行为空行时,就会将你指定的这个列名的值设为null,然后再向数据库中插入。  
  36.     }  
  37.     //修改数据  
  38.     public void Upata_data(Book bk){  
  39.         SQLiteDatabase  db=myDatabaseHlper.getWritableDatabase();  
  40.         ContentValues values=new ContentValues();  
  41.         values.put("price",bk.getPrice() );  
  42.         db.update("Book", values, "name =?"new String []{bk.getName()+""});  
  43.   
  44.     }  
  45.     //查询数据  
  46.     public  List query_data(){  
  47.         //查询Book表中的数据  
  48.         List<Book> list=new ArrayList<Book>();  
  49.         SQLiteDatabase  db=myDatabaseHlper.getWritableDatabase();  
  50.         ContentValues values=new ContentValues();  
  51.         Cursor cursor=db.query("Book"nullnullnullnullnullnull);   
  52.         if(cursor.moveToFirst()){  
  53.             //遍历cursor对象,取出数据并打印。  
  54.             do{  
  55.                 Integer id=cursor.getInt(cursor.getColumnIndex("id"));  
  56.                 String name=cursor.getString(cursor.getColumnIndex("name"));  
  57.                 String author=cursor.getString(cursor.getColumnIndex("author"));  
  58.                 double pages=cursor.getDouble(cursor.getColumnIndex("pages"));  
  59.                 Integer price=cursor.getInt(cursor.getColumnIndex("price"));  
  60.                 //System.out.println("名字"+name+"作者"+author+"书页"+pages+"价格"+price);  
  61.                 list.add(new Book(id, author, pages, price, name));  
  62.             }while(cursor.moveToNext());                  
  63.         }  
  64.         return list;  
  65.   
  66.     }  
  67.     //删除数据  
  68.     public void delete_data(Book bk){  
  69.         SQLiteDatabase  db=myDatabaseHlper.getWritableDatabase();  
  70.         ContentValues values=new ContentValues();  
  71.         db.delete("Book""pages >?"new String []{bk.getPages()+""});  
  72.     }  
  73. }  
怎么测试呢????? 在AndroidManifest.xml中加入
<instrumentation 
   android:name="android.test.InstrumentationTestRunner"
   android:targetPackage="com.example.sqllitehelp"
   ></instrumentation>

 <uses-library android:name="android.test.runner"/>


具体请看:
地方
[html]  view plain  copy
  1. <span style="color:#333333;"><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.sqllitehelp"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="17"  
  9.         android:targetSdkVersion="17" />  
  10.     </span><span style="color:#ff0000;"><instrumentation   
  11.         android:name="android.test.InstrumentationTestRunner"  
  12.         android:targetPackage="com.example.sqllitehelp"  
  13.         ></instrumentation></span><span style="color:#333333;">  
  14.     <application  
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name="com.example.sqllitehelp.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.         </span><span style="color:#ff0000;"><uses-library android:name="android.test.runner"/></span><span style="color:#333333;">  
  29.     </application>  
  30.   
  31. </manifest></span>  
三测试操作数据库是否成功
[java]  view plain  copy
  1. <span style="font-size:14px;">package com.sqlitehelp.text;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.example.entity.Book;  
  7. import com.example.sqllitehelp.dao.sqlitedao;  
  8.   
  9. import android.test.AndroidTestCase;  
  10.   
  11. public class SqlistTest  extends AndroidTestCase{  
  12.     public void testadd(){  
  13.         sqlitedao dao=new sqlitedao(getContext());  
  14.         Book  book=new Book(1"zhu"12.1512"codeadd");  
  15.         dao.add_data(book);  
  16.         System.out.println("添加数据成功");  
  17.     }  
  18.     public void query_add(){  
  19.         sqlitedao dao=new sqlitedao(getContext());  
  20.         List<Book> list=dao.query_data();  
  21.         System.out.println("查询数据成功"+list);  
  22.           
  23.     }  
  24. }</span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值