[1]熟悉MySQL的学这个就像会西瓜的人去学吃哈密瓜同样简单。
[2]若是对MySQL不太熟悉的童鞋,能够看一下个人这篇 :SpringBoot-14-MyBatis预热篇,MySQL小结
[3]SQLite:安卓内置轻量级的关系型数据库
[4]强烈建议语句什么的提早写好,在MySQL上测试一下,否则少个分号,多个逗号什么的就呵呵了
[5]安卓有API支持数据库操做,但感受不怎么灵活,感兴趣的能够本身了解一下
[1]:SQLite 不支持 DEFAULT 和 NOT NULL 连用(虽然连在一块儿也没啥用)java
[3]:INSERT INTO 的 INTO 要加上 (MySQL养成的坏毛病,得该)git
/** * 做者:张风捷特烈<br/> * 时间:2018/8/26 0026:14:48<br/> * 邮箱:1981462002@qq.com<br/> * 说明:SQL常量类 */ public class SQLCon { /** * 数据库名 */ public static String DB_NAME = "weapon"; /** * 数据库版本 */ public static int DB_VERSION = 1; /** * 建表语句 */ public static final String CREATE_TABLE = "CREATE TABLE sword (\n" + "id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n" + "name VARCHAR(32) NOT NULL,\n" + "atk SMALLINT UNSIGNED NOT NULL,\n" + "hit SMALLINT UNSIGNED NOT NULL DEFAULT 20,\n" + "crit SMALLINT UNSIGNED NOT NULL DEFAULT 10\n" + ");"; }
/** * 做者:张风捷特烈<br/> * 时间:2018/8/26 0026:14:26<br/> * 邮箱:1981462002@qq.com<br/> * 说明:个人数据库辅助类 */ public class MySQLHelper extends SQLiteOpenHelper { private Context mContext; /** * 构造函数 * * @param context 上下文 */ public MySQLHelper(Context context) { super(context, SQLCon.DB_NAME, null, SQLCon.DB_VERSION); mContext = context; } /** * 建立数据库,数据库存在就不会执行 * * @param db SQLite数据库对象 */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL(SQLCon.CREATE_TABLE);//建立表 } /** * 数据库进行升级 * * @param db SQLite数据库对象 * @param oldVersion 旧版本 * @param newVersion 新版本 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
MySQLHelper mySQLHelper = new MySQLHelper(this);//建立辅助对象 mySQLHelper.getWritableDatabase();//获取可写数据库对象 //getReadableDatabase()和getWritableDatabase() //这两个方法均可以建立或打开一个现有的数据库,并返回一个可对数据库进行读写操做的对象。 //磁盘空间已满时getWritableDatabase()异常
/** * 数据库版本 */ public static int DB_VERSION = 2; /** * 删除表语句 */ public static final String DROP_TABLE = "DROP TABLE sword";
db.execSQL(SQLCon.DROP_TABLE); L.d(oldVersion+":"+newVersion+L.l());//1:2
MySQLHelper mySQLHelper2 = new MySQLHelper(this);//建立辅助对象 mySQLHelper2.getWritableDatabase();//获取可写数据库对象
/** * 插入语句 */ public static final String INSERT = "INSERT INTO sword(id,name,atk,hit,crit) VALUES" + "(1,'痕兮',7000,800,999)," + "(2,'逐暮',100,1000,10000)," + "(3,'风跃',9000,10,255);";
mDb = new MySQLHelper(this).getWritableDatabase(); mDb.execSQL(SQLCon.INSERT);
/** * 删除数据 */ public static final String DELETE = "DELETE FROM sword WHERE id=1;";
mDb = new MySQLHelper(this).getWritableDatabase(); mDb.execSQL(SQLCon.DELETE);
/** * 修改数据 */ public static final String UPDATE = "UPDATE sword SET hit=hit+1;";
mDb = new MySQLHelper(this).getWritableDatabase(); mDb.execSQL(SQLCon.UPDATE);
Cursor cursor = mDb.rawQuery("SELECT * FROM sword", null); while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String atk = cursor.getString(cursor.getColumnIndex("atk")); String hit = cursor.getString(cursor.getColumnIndex("hit")); String crit = cursor.getString(cursor.getColumnIndex("crit")); System.out.println(id + "---" + name + "---" + atk + "---" + hit + "---" + crit); } //2---逐暮---100---1001---10000 //3---风跃---9000---11---255 cursor.close();//关闭游标
Cursor cursor2 = mDb.rawQuery("SELECT * FROM sword WHERE id = ?", new String[]{"2"}); while (cursor2.moveToNext()) { String id = cursor2.getString(cursor2.getColumnIndex("id")); String name = cursor2.getString(cursor2.getColumnIndex("name")); String atk = cursor2.getString(cursor2.getColumnIndex("atk")); String hit = cursor2.getString(cursor2.getColumnIndex("hit")); String crit = cursor2.getString(cursor2.getColumnIndex("crit")); System.out.println(id + "---" + name + "---" + atk + "---" + hit + "---" + crit); } //2---逐暮---100---1001---10000 cursor2.close();//关闭游标
/** * 建表语句 */ public static final String CREATE_TABLE = "CREATE TABLE sword (\n" + "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" + "name VARCHAR(32) NOT NULL,\n" + "atk SMALLINT UNSIGNED DEFAULT 1000,\n" + "hit SMALLINT UNSIGNED DEFAULT 20,\n" + "crit SMALLINT UNSIGNED DEFAULT 10\n" + ");";
/** * 做者:张风捷特烈<br/> * 时间:2018/8/26 0026:17:50<br/> * 邮箱:1981462002@qq.com<br/> * 说明:数据库操做类 */ public class SwordDao { private static SwordDao sSwordDao; private SQLiteDatabase db; /** * 私有化构造函数 */ private SwordDao() { } /** * 单例模式获取SwordDao * * @return SwordDao */ public static SwordDao get() { if (sSwordDao == null) { synchronized (SwordDao.class) { if (sSwordDao == null) { sSwordDao = new SwordDao(); } } } return sSwordDao; } public SwordDao attach(SQLiteDatabase db) { this.db = db; return this; } /** * 查询全部 * @return */ public List<Sword> findAll() { Cursor cursor = db.rawQuery("SELECT * FROM sword", null); List<Sword> swords = new ArrayList<>(); while (cursor.moveToNext()) { String tempId = cursor.getString(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String tempAtk = cursor.getString(cursor.getColumnIndex("atk")); String tempHit = cursor.getString(cursor.getColumnIndex("hit")); String tempCrit = cursor.getString(cursor.getColumnIndex("crit")); int id = tempId == null ? -1 : Integer.parseInt(tempId); int atk = tempAtk == null ? -1 : Integer.parseInt(tempAtk); int hit = tempHit == null ? -1 : Integer.parseInt(tempHit); int crit = tempCrit == null ? -1 : Integer.parseInt(tempCrit); Sword sword = new Sword(name, atk, hit, crit); sword.setId(id); swords.add(sword); } cursor.close();//关闭游标 return swords; } /** * 插入 * @param sword */ public void insert(Sword sword) { db.execSQL("INSERT INTO sword(name,atk,hit,crit) VALUES(?,?,?,?)", new String[]{ sword.getName(), sword.getAtk() + "", sword.getHit() + "", sword.getCrit() + ""}); } /** * 插入一个剑名称,其余默认 * * @param name 名称 */ public void insert(String name) { db.execSQL("INSERT INTO sword(name) VALUES(?)", new String[]{name}); } }
项目源码 | 日期 | 备注 |
---|---|---|
V0.1--无 | 2018-8-26 | 1-SI--安卓SQLite基础使用指南 |
V0.2--无 | 2018-10-23 | 增长其余知识点 |
笔名 | 微信 | 爱好 | |
---|---|---|---|
张风捷特烈 | 1981462002 | zdl1994328 | 语言 |
个人github | 个人简书 | 个人CSDN | 我的网站 |
1----本文由张风捷特烈原创,转载请注明 2----欢迎广大编程爱好者共同交流 3----我的能力有限,若有不正之处欢迎你们批评指证,一定虚心改正 4----看到这里,我在此感谢你的喜欢与支持