欠伸展肢体,吟咏心自愉。这篇文章主要讲述关于AndroidSQLite数据库后台处理的小例子相关的知识,希望能为你提供帮助。
- 首先我先创建一个包(package)entity
1 package com.example.sqlgameplayer.entity; 2 3 /** 4* Created by 徐伟杰 on 2016/9/25. 5* 游戏玩家的实体类 6*/ 7 8 public class GamePlayer { 9private int id; 10private String player; 11private int score; 12private int level; 13public GamePlayer(int id,String player,int score,int level){ 14this.id=id; 15this.player=player; 16this.score=score; 17this.level=level; 18} 19public GamePlayer(String player,int score,int level){ 20this.player=player; 21this.score=score; 22this.level=level; 23} 24public GamePlayer(){} 25 26public int getId() { 27return id; 28} 29 30public void setId(int id) { 31this.id = id; 32} 33 34public String getPlayer() { 35return player; 36} 37 38public void setPlayer(String player) { 39this.player = player; 40} 41 42public int getScore() { 43return score; 44} 45 46public void setScore(int score) { 47this.score = score; 48} 49 50public int getLevel() { 51return level; 52} 53 54public void setLevel(int level) { 55this.level = level; 56} 57 58@Override 59public String toString() { 60return "GamePlayer{" + 61"id=" + id + 62", player=‘" + player + ‘\‘‘ + 63", score=" + score + 64", level=" + level + 65‘}‘; 66} 67 }
- 创建包db,在db里创建类GameMetaData
1 package com.example.sqlgameplayer.db; 2 3 import android.provider.BaseColumns; 4 5 /** 6* Created by 徐伟杰 on 2016/9/25. 7* 数据库元数据 8*/ 9 10 public final class GameMetaData { 11private GameMetaData(){}; 12public static abstract class GamePlayer implements BaseColumns{ 13public staticfinal String TABLE_NAME="player_table"; 14public staticfinal String PLAYER="player"; 15public static finalString SCORE="score"; 16public staticfinal String LEVEL="level"; 17} 18 }
- 包db创建类DatabaseHelper
1 package com.example.sqlgameplayer.db; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 7 /** 8* Created by 徐伟杰 on 2016/9/25. 9*/ 10 11 public class DatabaseHelper extends SQLiteOpenHelper { 12private static final String DB_NAME="game.db"; //数据库名字 13private static final int VERSION=1; //版本号 14private static final String CREATE_TABLE_PLAYER="CREATE TABLE IF NOT EXISTS player_table("+//如果没有表单创建表单player_table 15"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+//id,主键自动增加 16"player TEXT,score INTEGER,level INTEGER)"; //数据的类型 17 18private static final String DROP_TABLE_PLAYER="DROP TABLE IF EXISTS player_table"; 19public DatabaseHelper(Context context) { 20//context,数据库名字,游标,版本号 21super(context, DB_NAME, null, VERSION); 22} 23 24@Override 25public void onCreate(SQLiteDatabase sqLiteDatabase) { 26sqLiteDatabase.execSQL(CREATE_TABLE_PLAYER); 27} 28 //更新 29@Override 30public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {//i代表老版本,i1代表新版本 31sqLiteDatabase.execSQL(DROP_TABLE_PLAYER); 32sqLiteDatabase.execSQL(CREATE_TABLE_PLAYER); 33//先删除后创建 34} 35 }
【关于AndroidSQLite数据库后台处理的小例子】
- db包创建类DatabaseAdapter
1 package com.example.sqlgameplayer.db; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 8 9 import com.example.sqlgameplayer.entity.GamePlayer; 10 11 import java.util.ArrayList; 12 13 /** 14* Created by 徐伟杰 on 2016/9/25. 15* 数据库的工具类 16*/ 17 18 public class DatabaseAdapter { 19private DatabaseHelper dbHelper; 20public DatabaseAdapter(Context context){dbHelper=new DatabaseHelper(context); } 21//添加操作 22public void add(GamePlayer gamePlayer){ 23//获取可写的数据库 24SQLiteDatabase db=dbHelper.getWritableDatabase(); 25ContentValues values=new ContentValues(); 26values.put(GameMetaData.GamePlayer.PLAYER,gamePlayer.getPlayer()); 27values.put(GameMetaData.GamePlayer.SCORE,gamePlayer.getScore()); 28values.put(GameMetaData.GamePlayer.LEVEL,gamePlayer.getLevel()); 29db.insert(GameMetaData.GamePlayer.TABLE_NAME,null,values); 30db.close(); 31} 32//删除操作(实际开发应用中是不会删除的,而是隐藏) 33public void delete(int id){ 34SQLiteDatabase db=dbHelper.getWritableDatabase(); 35String whereClause=GameMetaData.GamePlayer._ID+"=?"; 36String[] whereArgs={String.valueOf(id)}; 37//表名,条件,条件的值 38db.delete(GameMetaData.GamePlayer.TABLE_NAME,whereClause,whereArgs); 39db.close(); 40} 41//更新操作 42public void update(GamePlayer gamePlayer){ 43SQLiteDatabase db=dbHelper.getWritableDatabase(); 44ContentValues values=new ContentValues(); 45values.put(GameMetaData.GamePlayer.PLAYER,gamePlayer.getPlayer()); 46values.put(GameMetaData.GamePlayer.SCORE,gamePlayer.getScore()); 47values.put(GameMetaData.GamePlayer.LEVEL,gamePlayer.getLevel()); 48String whereClause=GameMetaData.GamePlayer._ID+"=?"; 49String[] whereArgs={String.valueOf(gamePlayer.getId())}; 50db.update(GameMetaData.GamePlayer.TABLE_NAME,values,whereClause,whereArgs); 51db.close(); 52} 53//查询 54public GamePlayer findById(int id) { 55//获取只读 56SQLiteDatabase db=dbHelper.getReadableDatabase(); 57//属性:是否可重复,表名,读多少个列表(这里数据比较少所以填null),条件,条件值,分组,排序等等 58Cursor c=db.query(true, GameMetaData.GamePlayer.TABLE_NAME,null, GameMetaData.GamePlayer._ID+"=?",new String[]{String.valueOf(id)},null,null,null,null); 59GamePlayer gamePlayer=null; 60if(c.moveToNext()) { 61gamePlayer=new GamePlayer(); 62gamePlayer.setId(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer._ID))); 63gamePlayer.setPlayer(c.getString(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.PLAYER))); 64gamePlayer.setScore(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.SCORE))); 65gamePlayer.setLevel(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.LEVEL))); 66} 67c.close(); 68db.close(); 69 70return gamePlayer; 71} 72//查询所有 73public ArrayList< GamePlayer> findAll(){ 74SQLiteDatabase db=dbHelper.getReadableDatabase(); 75//收集 _id,player,score,level(注意这里最好不要使用*号,回影响性能)从paly_table 排序通过score 降序 76String sql="select _id,player,score,level from player_table order by score desc"; 77Cursor c=db.rawQuery(sql,null); 78ArrayList< GamePlayer> gamePlayers=new ArrayList< > (); 79GamePlayer gamePlayer=null; 80while (c.moveToNext()){ 81gamePlayer=new GamePlayer(); 82gamePlayer.setId(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer._ID))); 83gamePlayer.setPlayer(c.getString(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.PLAYER))); 84gamePlayer.setScore(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.SCORE))); 85gamePlayer.setLevel(c.getInt(c.getColumnIndexOrThrow(GameMetaData.GamePlayer.LEVEL))); 86gamePlayers.add(gamePlayer); 87} 88c.close(); 89db.close(); 90return gamePlayers; 91} 92//获取总记录数 93public int getCount(){ 94int count=0; 95String sql="select count(_id) from player_table"; 96SQLiteDatabase db=dbHelper.getReadableDatabase(); 97Cursor c=db.rawQuery(sql,null); 98c.moveToFirst(); 99count=c.getInt(0); 100c.close(); 101db.close(); 102return count; 103} 104 }
推荐阅读
- Android开发(最全面最易懂的Android屏幕适配解决方案)
- Android 四大组件 Service 服务
- Android开发之手把手教你写ButterKnife框架
- 百度-Android-二面小结
- android千变万化的ViewPager之四
- Android Service演义
- Week2 Bing词典Android客户端案例分析
- html5 SVG绘制以及和canvas的区别 – HTML5教程
- html媒体类型和字符编码参考文档 – HTML教程