1.将事先准备好的数据库模板放在model的资源文件夹中2 权限uses-permission android:nameandroid.permission.WRITE_EXTERNAL_STORAGE / uses-permission android:nameandroid.permission.READ_EXTERNAL_STORAGE /3.依赖4.先键一个类MySQLiteHelper.java功能识别系统路径下是否存在SQLite数据库 /data/user/0/com.example.testa/databases/data.db如果没有将工程下 文件夹res-raw-data.db复制到系统该路径。import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.rx.mytest1.R; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; public class MySQLiteHelper { static String TAG MySQLiteHelper; private Context context; static String fileName data.db; // public static SQLiteDatabase getDatabase(Context context){ String TAG context.getClass().getName(); try{ //String path0 context.getApplicationContext().getFilesDir().getAbsolutePath(); File filePath context.getDatabasePath(fileName); //data/user/0/com.example.testa/databases/data.db if(!filePath.exists()){ Log.e(TAG, getDatabase: 从raw中复制文件到 filePath.toString()); try{ InputStream is context.getResources().openRawResource(R.raw.data); FileOutputStream fos new FileOutputStream(filePath); byte[] buffer new byte[8192]; int count 0; while ((count is.read(buffer)) 0){ fos.write(buffer, 0,count); } fos.close(); is.close(); }catch (Exception e){ Log.e(TAG, getDatabase错误1: e) ; } } else{ Log.e(TAG, getDatabase: 数据库已存在); } SQLiteDatabase mysql SQLiteDatabase.openOrCreateDatabase(filePath,null); return mysql; }catch (Exception e){ Log.e(TAG, getDatabase错误1: e) ; e.printStackTrace(); } return null; } }5. 新建工具类 SQliteManager_Curve.javapublic class SQliteManager_Curve { protected String tabel_name YZK_2603_curve_list; String TAG getClass().getName(); Context context; 方法1 方法1 。。。。。略 }5.1 插入数据 SQliteManager_Curve.java类中的方法insertData();public void insertData(BeanYZKCurve bean) { SQLiteDatabase db MySQLiteHelper.getDatabase(context); ContentValues values contentValuesLoadData(bean); db.insert(tabel_name, null, values);//执行插入操作 } private ContentValues contentValuesLoadData(BeanYZKCurve bean){ ContentValues values new ContentValues(); values.put(curveId, bean.getCurveId()); values.put(productId, bean.getProductId()); values.put(productName, bean.getProductName()); values.put(agrochemicaleId, bean.getAgrochemicaleId()); values.put(agrochemicaleName,bean.getAgrochemicaleName()); values.put(agrochemicaleNo, bean.getAgrochemicaleNo()); values.put(matrix, bean.getMatrix()); values.put(diluent, bean.getDiluent()); values.put(cardType, bean.getCardType()); values.put(algorithm, bean.getAlgorithm()); //节省空间 略 return values; }5.2 读数据 SQliteManager_Curve.java类中的方法read()public ArrayListBeanYZKCurve read(String str){ //单条件模糊查询多列查询如 str”H” “name”列 或 “type”列 或 “fristPY”列 // 中有 “h”相关字的都将查询出来如h hh h1 Cursor cursor; ArrayListBeanYZKCurve listBeans new ArrayList(); SQLiteDatabase db MySQLiteHelper.getDatabase(context); //--------------------------------------- String[] columns null; //String selection company_name like % str% or first_letter like % str%; String selection null; String[] selectionArgs null; cursordb.query(tabel_name, columns, selection, selectionArgs, null, null, null); //--------------------------------------- while (cursor.moveToNext()){ listBeans.add(BeanSetData(cursor)); } if(cursor!null)cursor.close(); //--------------------------------------- return listBeans; } private BeanYZKCurve BeanSetData(Cursor c){ BeanYZKCurve bean new BeanYZKCurve(); //int id c.getInt(c.getColumnIndex(id)); //bean.set_id(String.valueOf(id)); bean.setCurveId(c.getInt(c.getColumnIndex(curveId))); bean.setProductId(c.getInt(c.getColumnIndex(productId))); bean.setProductName(c.getString(c.getColumnIndex(productName))); bean.setAgrochemicaleId(c.getInt(c.getColumnIndex(agrochemicaleId))); bean.setAgrochemicaleName(c.getString(c.getColumnIndex(agrochemicaleName))); bean.setAgrochemicaleNo(c.getInt(c.getColumnIndex(agrochemicaleNo))); bean.setMatrix(c.getInt(c.getColumnIndex(matrix))); bean.setDiluent(c.getInt(c.getColumnIndex(diluent))); bean.setCardType(c.getInt(c.getColumnIndex(cardType))); bean.setAlgorithm(c.getInt(c.getColumnIndex(algorithm))); //略 return bean; }5.3 删除表单而不是清除表单内容 SQliteManager_Curve.java类中的方法public void delectSheet(){ try{ SQLiteDatabase db MySQLiteHelper.getDatabase(context); db.execSQL(DELETE FROM tabel_name); Log.e(TAG, delectSheet: 删除表单ok ); }catch (Exception e){ Log.e(TAG, delectSheet: 删除表单错误 ); } }5.4 清空表单内容而不是删除表单 SQliteManager_Curve.java类中的方法public void clearSheet(){ try{ SQLiteDatabase db MySQLiteHelper.getDatabase(context); db.execSQL(DELETE FROM tabel_name); db.beginTransaction(); try { db.delete(tabel_name, null, null); db.setTransactionSuccessful(); } finally { db.endTransaction(); } db.execSQL(VACUUM); }catch (Exception e){ Log.e(TAG, 清空-错误 ); } }5.5 获取表单-总数量SQliteManager_Curve.java类中的方法public int getCount(){ try{ SQLiteDatabase db MySQLiteHelper.getDatabase(context); // 查询所有数据 Cursor cursor db.rawQuery(SELECT * FROM tabel_name, null); int count cursor.getCount(); // 直接获取Cursor中的记录数 cursor.close(); db.close(); Log.e(TAG,getCount()数量: count); return count; }catch (Exception e){ Log.e(TAG,getCount()错误:); return -1; }等待更新
android-实例1-数据库sqlite(依赖sqlite)
1.将事先准备好的数据库模板放在model的资源文件夹中2 权限uses-permission android:nameandroid.permission.WRITE_EXTERNAL_STORAGE / uses-permission android:nameandroid.permission.READ_EXTERNAL_STORAGE /3.依赖4.先键一个类MySQLiteHelper.java功能识别系统路径下是否存在SQLite数据库 /data/user/0/com.example.testa/databases/data.db如果没有将工程下 文件夹res-raw-data.db复制到系统该路径。import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.rx.mytest1.R; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; public class MySQLiteHelper { static String TAG MySQLiteHelper; private Context context; static String fileName data.db; // public static SQLiteDatabase getDatabase(Context context){ String TAG context.getClass().getName(); try{ //String path0 context.getApplicationContext().getFilesDir().getAbsolutePath(); File filePath context.getDatabasePath(fileName); //data/user/0/com.example.testa/databases/data.db if(!filePath.exists()){ Log.e(TAG, getDatabase: 从raw中复制文件到 filePath.toString()); try{ InputStream is context.getResources().openRawResource(R.raw.data); FileOutputStream fos new FileOutputStream(filePath); byte[] buffer new byte[8192]; int count 0; while ((count is.read(buffer)) 0){ fos.write(buffer, 0,count); } fos.close(); is.close(); }catch (Exception e){ Log.e(TAG, getDatabase错误1: e) ; } } else{ Log.e(TAG, getDatabase: 数据库已存在); } SQLiteDatabase mysql SQLiteDatabase.openOrCreateDatabase(filePath,null); return mysql; }catch (Exception e){ Log.e(TAG, getDatabase错误1: e) ; e.printStackTrace(); } return null; } }5. 新建工具类 SQliteManager_Curve.javapublic class SQliteManager_Curve { protected String tabel_name YZK_2603_curve_list; String TAG getClass().getName(); Context context; 方法1 方法1 。。。。。略 }5.1 插入数据 SQliteManager_Curve.java类中的方法insertData();public void insertData(BeanYZKCurve bean) { SQLiteDatabase db MySQLiteHelper.getDatabase(context); ContentValues values contentValuesLoadData(bean); db.insert(tabel_name, null, values);//执行插入操作 } private ContentValues contentValuesLoadData(BeanYZKCurve bean){ ContentValues values new ContentValues(); values.put(curveId, bean.getCurveId()); values.put(productId, bean.getProductId()); values.put(productName, bean.getProductName()); values.put(agrochemicaleId, bean.getAgrochemicaleId()); values.put(agrochemicaleName,bean.getAgrochemicaleName()); values.put(agrochemicaleNo, bean.getAgrochemicaleNo()); values.put(matrix, bean.getMatrix()); values.put(diluent, bean.getDiluent()); values.put(cardType, bean.getCardType()); values.put(algorithm, bean.getAlgorithm()); //节省空间 略 return values; }5.2 读数据 SQliteManager_Curve.java类中的方法read()public ArrayListBeanYZKCurve read(String str){ //单条件模糊查询多列查询如 str”H” “name”列 或 “type”列 或 “fristPY”列 // 中有 “h”相关字的都将查询出来如h hh h1 Cursor cursor; ArrayListBeanYZKCurve listBeans new ArrayList(); SQLiteDatabase db MySQLiteHelper.getDatabase(context); //--------------------------------------- String[] columns null; //String selection company_name like % str% or first_letter like % str%; String selection null; String[] selectionArgs null; cursordb.query(tabel_name, columns, selection, selectionArgs, null, null, null); //--------------------------------------- while (cursor.moveToNext()){ listBeans.add(BeanSetData(cursor)); } if(cursor!null)cursor.close(); //--------------------------------------- return listBeans; } private BeanYZKCurve BeanSetData(Cursor c){ BeanYZKCurve bean new BeanYZKCurve(); //int id c.getInt(c.getColumnIndex(id)); //bean.set_id(String.valueOf(id)); bean.setCurveId(c.getInt(c.getColumnIndex(curveId))); bean.setProductId(c.getInt(c.getColumnIndex(productId))); bean.setProductName(c.getString(c.getColumnIndex(productName))); bean.setAgrochemicaleId(c.getInt(c.getColumnIndex(agrochemicaleId))); bean.setAgrochemicaleName(c.getString(c.getColumnIndex(agrochemicaleName))); bean.setAgrochemicaleNo(c.getInt(c.getColumnIndex(agrochemicaleNo))); bean.setMatrix(c.getInt(c.getColumnIndex(matrix))); bean.setDiluent(c.getInt(c.getColumnIndex(diluent))); bean.setCardType(c.getInt(c.getColumnIndex(cardType))); bean.setAlgorithm(c.getInt(c.getColumnIndex(algorithm))); //略 return bean; }5.3 删除表单而不是清除表单内容 SQliteManager_Curve.java类中的方法public void delectSheet(){ try{ SQLiteDatabase db MySQLiteHelper.getDatabase(context); db.execSQL(DELETE FROM tabel_name); Log.e(TAG, delectSheet: 删除表单ok ); }catch (Exception e){ Log.e(TAG, delectSheet: 删除表单错误 ); } }5.4 清空表单内容而不是删除表单 SQliteManager_Curve.java类中的方法public void clearSheet(){ try{ SQLiteDatabase db MySQLiteHelper.getDatabase(context); db.execSQL(DELETE FROM tabel_name); db.beginTransaction(); try { db.delete(tabel_name, null, null); db.setTransactionSuccessful(); } finally { db.endTransaction(); } db.execSQL(VACUUM); }catch (Exception e){ Log.e(TAG, 清空-错误 ); } }5.5 获取表单-总数量SQliteManager_Curve.java类中的方法public int getCount(){ try{ SQLiteDatabase db MySQLiteHelper.getDatabase(context); // 查询所有数据 Cursor cursor db.rawQuery(SELECT * FROM tabel_name, null); int count cursor.getCount(); // 直接获取Cursor中的记录数 cursor.close(); db.close(); Log.e(TAG,getCount()数量: count); return count; }catch (Exception e){ Log.e(TAG,getCount()错误:); return -1; }等待更新