第一种为: 通过继承SQLiteOpenHelper 这个抽象类
class MySQLiteHelper extends SQLiteOpenHelper{ public MySQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String str = "CREATE TABLE "+ tableName +"(" + "taskID VARCHAR PRIMARY KEY," + "accidentAddress VARCHAR," + "accidentReportedPeople VARCHAR," + "accidentReportedPhoneNumber VARCHAR," + "accidentPlateNumber VARCHAR," + "accidentReportedNumber VARCHAR," + "customerRank VARCHAR," + "accidentNumberOfTimes VARCHAR," + "taskState VARCHAR," + "taskTime VARCHAR)"; try { db.execSQL(str); Log.i("TaskSavaDataBase", "成功创建表"); } catch (Exception e) { Log.i("TaskSavaDataBase", "创建表失败"); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ } }
然后再实例化这个类的对象,就创建的一个数据库
MySQLiteHelper sqlHelper = new MySQLiteHelper(this.context, dataBaseName, null, 1); data = sqlHelper.getWritableDatabase();
由于SQLiteOpenHelper是一个抽象类,我们创建一个数据库时,重载的onCreate()方法在什么时候被调用呢?答案为:在数据库第一次被创建的时候被调用,如果这个数据库已经存在了,这个方法则不会被调用。onUpgrade()方法是在数据库的version改变时,则会自动调用此方法,以用来更新数据库,这时就需重写这个方法。
第二种:通过SQLiteDataBase.openOrCreateDatabase()这个方法来创建数据库
/** 创建SQLite数据CallData*/ public void createDataBase(){ String path = "/mnt/sdcard/CallData.db"; File file = new File(path); data = SQLiteDatabase.openOrCreateDatabase(file, null); //data = context.openOrCreateDatabase(name, mode, null); //data的创建方法注释和未注释的都可以,事不过实现的方式不同而已,有哪些不同请看我另外一篇关于SQLite数据库的博文 }
通过这个方法必须手动调用一个创建表的函数,使用SQLiteOpenHelper 在创建数据库的时候,会自动调用onCreate方法帮你创建好一个表,再次启动时则不会创建。
SQLiteDataBase 常用的方法如下:
下面介绍一个常用的操作方法:
插入数据:
这个方法用于当你插入的数据不是写死的
/** * 保存任务*/ public void InsertTaskData(Task_Picc task){ try { String str = "INSERT INTO " + tableName +" VALUES (?,?,?,?,?,?,?,?,?,?)"; //注意每个字母之间都必须空格,尤其是参数和字符之间的空格 如:"INSERT INTO " + tableName +,INTO后必须有一个空格 //博主我当初就被坑了 String obj[] = new String[]{ task.getTaskId(), task.getAccidentAddress(), task.getAccidentReportedPeople(), task.getAccidentReportedPhoneNumber(), task.getAccidentPlateNumber(), task.getAccidentReportedNumber(), task.getCustomerRank(), task.getAccidentNumberOfTimes(), task.getTaskState(), task.getTaskTime() }; data.execSQL(str, obj); Log.i("TaskSaveDataBase", "保存成功"); } catch (Exception e) { Log.i("TaskSaveDataBase", "添加任务异常"); } }
下面这种插入方法,其插入的数据时写死,不能以参数的形式传入:
public void InsertTaskData1(){ String str = "INSERT INTO"+tableName+" (" + taskID +"," + accidentAddress + " )VALUES('234','fdf')"; //VALUES 后面括号类的数据必须为字符,当初我是以参数的形式填进去的,怎么弄怎么不对 try { data.execSQL(str); Log.i("TaskSaveDataBase", "插入成功"); } catch (Exception e) { Log.i("TaskSaveDataBase", "插入失败"); } }
还有一个插入方法,也能用参数的形式传入:
public void InsertTaskData2(){ ContentValues cv = new ContentValues(); cv.put(taskID,"2344"); cv.put(accidentAddress, "dfd"); try { data.insertOrThrow(tableName,null,cv); Log.i("TaskSaveDataBase", "插入成功"); } catch (SQLException e) { Log.i("TaskSaveDataBase", "插入失败"); } }
更新数据:
/** * 更新任务状态*/ public void UpdateTaskStateData(String state,String taskId){ try { String str = "UPDATE " + tableName + " SET taskState ="+"'"+state+"'" + " WHERE " + "taskID =" +"'"+taskId+"'"; data.execSQL(str); } catch (Exception e) { Log.i("TaskSavaDataBase", "插入任务状态数据异常"); } }
删除数据:
/** * 删除任务数据*/ public void DeleteTaskData(String taskId){ try { String str = "DROP FROM " + tableName + " WHERE taskID=" +"'"+taskId+"'"; data.execSQL(str); } catch (Exception e) { Log.i("TaskSaveDataBase", "删除任务异常"); } }
查询数据:
/** * 查询历史任务信息*/ public List<Task_Picc> QueryData(String taskId){ Task_Picc task_picc = new Task_Picc(); List<Task_Picc> list = new ArrayList<Task_Picc>(); String sql = "SELECT * FROM "+tableName+" WHERE taskID="+"'"+taskId+"'"; Cursor cursor = data.rawQuery(sql, null); Log.i("TaskSaveDataBase", "准备查询"+cursor.getCount()); if(cursor.moveToFirst()){ Log.i("TaskSaveDataBase", "正在查询"); do{ Log.i("TaskSaveDataBase", "查询成功"); task_picc.setTaskId(cursor.getString(0)+"ff"); task_picc.setAccidentAddress(cursor.getString(1)+"ff"); task_picc.setAccidentReportedPeople(cursor.getString(2)+"ff"); task_picc.setAccidentReportedPhoneNumber(cursor.getString(3)+"ff"); task_picc.setAccidentPlateNumber(cursor.getString(4)+"ff"); task_picc.setAccidentReportedNumber(cursor.getString(5)+"ff"); task_picc.setCustomerRank(cursor.getString(6)+"ff"); task_picc.setAccidentNumberOfTimes(cursor.getString(7)+"ff"); task_picc.setTaskState(cursor.getString(8)+"ff"); task_picc.setTaskTime(cursor.getString(9)+"ff"); list.add(task_picc); }while(cursor.moveToNext()); cursor.close(); }; return list; }
其实对SQLiteDataBase进行操作还能够其自带的数据库操作方法,只要传入对应的参数就行。具体参考这篇文章:http://www.cnblogs.com/TerryBlog/archive/2010/06/12/1757166.html
如果有时候SQL语句忘记写了,可以上这个网站去查询:
http://www.w3school.com.cn/sql/sql_insert.asp
参考文章:
http://aina-hk55hk.iteye.com/blog/698794
http://www.efan-gz.com/2011/07/android-sqlitedatabase/