Android 继承SQLiteOpenHelper自定义DBHelper存取数据与图像

 

Android 继承SQLiteOpenHelper自定义DBHelper存取数据与图像如下:


 
 
  1. package com.test;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. public class DBHelper extends SQLiteOpenHelper {
  12. private static final String DB_NAME = "test.db";
  13. private static final int DB_VERSION = 1;
  14. private static final String TABLE_NAME = "info";
  15. private static final String CREATE_INFO = "create table if not exists info("
  16. + "id integer primary key autoincrement,name varchar(20),"
  17. + "time varchar(20),img BLOB)";
  18. private SQLiteDatabase db;
  19. DBHelper(Context c) {
  20. //
  21. super(c, DB_NAME, null, DB_VERSION);
  22. }
  23. @Override
  24. public void onCreate(SQLiteDatabase db) {
  25. this.db = db;
  26. db.execSQL(CREATE_INFO);
  27. }
  28. public void insert(ContentValues values, String tableName) {
  29. db = getWritableDatabase();
  30. db.insert(tableName, null, values);
  31. db.close();
  32. }
  33. // Return cursor with all columns by tableName
  34. public Cursor query(String tableName) {
  35. db = getWritableDatabase();
  36. Cursor c = db.query(tableName, null, null, null, null, null, null);
  37. return c;
  38. }
  39. // Return cursor by SQL string
  40. public Cursor rawQuery(String sql, String[] args) {
  41. db = getWritableDatabase();
  42. Cursor c = db.rawQuery(sql, args);
  43. return c;
  44. }
  45. // Execute a single SQL statement(as insert,create,delete)instead of a query
  46. public void execSQL(String sql) {
  47. db = getWritableDatabase();
  48. db.execSQL(sql);
  49. }
  50. // Delete by id
  51. public void del(int id) {
  52. if (db == null)
  53. db = getWritableDatabase();
  54. db.delete(TABLE_NAME, "id=?", new String[] { String.valueOf(id) });
  55. }
  56. public void close() {
  57. if (db != null)
  58. db.close();
  59. }
  60. @Override
  61. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  62. }
  63. // Bitmap to byte[]
  64. public byte[] bmpToByteArray(Bitmap bmp) {
  65. // Default size is 32 bytes
  66. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  67. try {
  68. bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
  69. bos.close();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. return bos.toByteArray();
  74. }
  75. // Cursor to bitmap
  76. Bitmap cursorToBmp(Cursor c, int columnIndex) {
  77. byte[] data = c.getBlob(columnIndex);
  78. try {
  79. return BitmapFactory.decodeByteArray(data, 0, data.length);
  80. } catch (Exception e) {
  81. return null;
  82. }
  83. }
  84. }

DBhelper调用方法:


 
 
  1. //定义helper
  2. private static DBHelper helper;
  3. //创建helper
  4. helper = new DBHelper( this);
  5. //插入数据与图像
  6. ContentValues values = new ContentValues();
  7. values.put( "name", "test");
  8. values.put( "img", helper.bmpToByteArray(bmp));
  9. helper.insert(values, "info");
  10. //访问数据与图像
  11. Cursor c = helper.rawQuery( "select * from info", null);
  12. c.moveToLast();
  13. String name = c.getString(c.getColumnIndex( "name"));
  14. Bitmap bmp = cursorToBmp(c, c.getColumnIndex( "img"));






 

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
										if(!window.csdn.anonymousUserLimit.judgment()){
											window.csdn.anonymousUserLimit.Jumplogin();
											return false;
										}else if(!currentUserName){
											window.csdn.anonymousUserLimit.updata();
										}
									}
									
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值