Sqlite的使用和一个简单的书籍管理系统(上),sqlite书籍管理系统
虽然说app与网络打交道比较多但是本地存储数据库sqlite还是有它很重要的责任
那来一起学习一下sqlite吧
android的存储方式有如下几种
http://blog.csdn.net/a351945755/article/details/50900652
等我有时间就整理下
sharedpreferences
文件存储
sqlite数据库
contentprovider
网络
我们来看看sqlite
他通过SQLiteOpenHelper这个抽象类;来创建和打开数据库
public class DBHelper extends SQLiteOpenHelper {
private static final String DATA_NAME = "jaytang.db";
private static final int version = 1;// 版本
private static final String TAG = "DBHelper";// 版本
public DBHelper(Context context) {
super(context, DATA_NAME, null, version);
// TODO Auto-generated constructor stub
}
/**
* 数据库第一次创建时回调该方法,一般做数据库的初始化操作 建表,添加数据等 SQLiteDatabase:增删查改
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.v(TAG, "创建表");
db.execSQL("create table t_book(_id integer primary key autoincrement,name text,price integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
重写一个DBhelper来继承SQLiteOpenHelper
并重写他里面的方法;在oncreate方法里面新建一个表
db.execSQL("create table t_book(_id integer primary key autoincrement,name text,price integer)");
这样我们就新建了一个表了
可以在 project Explorer里面查看我们新建的数据库jaytang.db
再来写一个工具类来实现数据库的增删查改方法
通过数据库的辅助类SQLiteDatabase来操作
public List
上面的工具类DButils能基本实现增删改查的效果
//工具类是可以复用的 所以尽量写好,并且进行单元测试
布局文件如下
" data-snippet-id="ext.4459fb8d606557a522e3584ea33cac61" data-snippet-saved="false" data-codota-status="done"><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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="书籍管理系统"
android:textColor="#0000ff"
android:textSize="22sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
style="@style/MyTitleStyle"
android:text="编号" />
<TextView
style="@style/MyTitleStyle"
android:layout_weight="2"
android:text="书名" />
<TextView
style="@style/MyTitleStyle"
android:text="价格" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_menu_delete"
android:visibility="invisible" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000ff"
android:dividerHeight="2dp" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/name_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="书名" />
<EditText
android:id="@+id/price_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="价格"
android:inputType="number" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="添加" />
</LinearLayout>
</LinearLayout>
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。