<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/LearnMysqL" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
<iframe frameborder="0" height="1500" name="ifd" scrolling="auto" src="https://mnifdv.cn/resource/cnblogs/LearnMysqL" width="100%"></iframe>
说明
android手机自带本地数据库 sqlite ,操作本地数据库android给了api
不过这节的重点是讲解如何使用 郭霖(第一行代码) 写的litepal 操作数据库
在使用litepal之前,咱们先看下使用android 自带的api操作数据库
打开本节代码
1.用户可以去百度了...这是Android api操作数据库最基本的规定
public class DataBase extends sqliteOpenHelper { private final static String TABLE_NAME = "MyTable";//表格名字 private final static String ID = "id"; //第一个字段 private sqliteDatabase db = null; public final static String UserName = "name";//第二个字段 public DataBase(Context context, String DATABASE_NAME, int DATABASE_VERSION) { super(context,DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(sqliteDatabase db) { // Todo Auto-generated method stub String sql = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER primary key autoincrement, " + UserName + " text "+ ")"; db.execsql(sql); } @Override public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { // Todo Auto-generated method stub String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; db.execsql(sql); onCreate(db); } /** * insert data * @param name * @return */ public long insert(String name) { db = getWritableDatabase(); /* ContentValues */ ContentValues cv = new ContentValues(); cv.put(UserName, name); long row = db.insert(TABLE_NAME, null, cv); db.close(); return row; } /** * delete data * @param name * @return */ public long delete(String name) { db = getWritableDatabase(); String where = UserName + " = ?"; String[] whereValue ={name}; long row = db.delete(TABLE_NAME, where, whereValue); db.close(); return row; } /** * query data * @param name * @return */ public Cursor query(String name) { db = getWritableDatabase(); Cursor cursor = null; String str = null; str = "select "+name +" from "+ TABLE_NAME; cursor = db.rawQuery(str,null); return cursor; } }
2.上面的程序是生成这样一张表格
3.插入数据
4.删除数据
5.查询数据
使用
接着看litepal
1.把jar包引入自己工程
我使用的是2.0版本
public class DataBaseLitepal extends LitePalSupport { private int id;//ID private String UserName;//用户名 private String UserPwd;//密码public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return UserName; } public void setUserName(String userName) { UserName = userName; } public String getUserPwd() { return UserPwd; } public void setUserPwd(String userPwd) { UserPwd = userPwd; } }
上面的意思是建一张这样的表格
3.在assets文件夹里面建一个litepal.xml文件
如果没有 assets 文件夹,建一个文件夹,assets 是放android资源的文件夹
<mapping class="com.example.myapplication.myapplicationlitepal.DataBaseLitepal"></mapping>
里面的calss需要写全路径.
<?xml version="1.0" encoding="utf-8"?> <litepal> <!-- 数据库名 --> <dbname value="Testlitepal" ></dbname> <!-- 数据库版本 --> <version value="1" ></version> <!--数据库文件路径,把所有的数据库文件放到此处 --> <list> <mapping class="com.example.myapplication.myapplicationlitepal.DataBaseLitepal"></mapping> </list> </litepal>
4.初始化
1.建一个 MyApplication 继承 Application
在 onCreate 里面写上下面两句
注: Application onCreate 是程序一启动就执行的函数,目的是让litepal第一时间初始化
如果自己有了 Application,只需要添加下面两句即可
/*=================litepal数据库=====================*/ LitePal.initialize(this); //获取到sqliteDatabase的实例,创建数据库表 sqliteDatabase db = LitePal.getDatabase();
2.在 AndroidManifest.xml 文件中配置下
/*添加数据*/ DataBaseLitepal dataBaseLitepal = new DataBaseLitepal(); dataBaseLitepal.setUserName("yang"); dataBaseLitepal.setUserPwd("11223344"); dataBaseLitepal.save(); /*查询数据*/ List<DataBaseLitepal> allSongs = LitePal.where("UserName=?","yang").find(DataBaseLitepal.class); for (DataBaseLitepal dataBaseLitepal1 :allSongs) { int id = dataBaseLitepal1.getId(); String UserName = dataBaseLitepal1.getUserName(); String UserPwd = dataBaseLitepal1.getUserPwd(); Log.d("MainActivity",id+" "+ UserName+ " " + UserPwd); }
/*修改数据:把用户yang的密码改为1234567890*/ ContentValues values = new ContentValues(); values.put("UserPwd","1234567890"); int updateallNum = LitePal.updateall(DataBaseLitepal.class,values,"UserName = ? ","yang"); /*查询数据*/ List<DataBaseLitepal> dataBaseLitepals = LitePal.where("UserName=?","yang").find(DataBaseLitepal.class); for (DataBaseLitepal dataBaseLitepal1 :dataBaseLitepals) { int id = dataBaseLitepal1.getId(); String UserName = dataBaseLitepal1.getUserName(); String UserPwd = dataBaseLitepal1.getUserPwd(); Log.d("MainActivity修改后查询",id+" "+ UserName+ " " + UserPwd); } /*删除用户名为yang,密码为1234567890的数据 */ int delAllNum = LitePal.deleteall(DataBaseLitepal.class,"UserName = ? and UserPwd = ? ","yang","1234567890");
7.更新数据库
1.假设后期项目需求增加了手机号
2.在litepal设置一个新版本即可
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。