//创建数据库,数据库名称为mydata.db
DatabaseHelper dbHelper = new DatabaseHelper(MyActivity.this, "mydata.db");
SQLiteDatabase db = dbHelper.getReadableDatabase();
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context, String name, CursorFactory cursorFactory, int version) {
super(context, name, cursorFactory, version);
}
public DatabaseHelper(Context context, String name) { //创建时只传入两个参数,可以不重写
this(context, name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO 创建数据库后,对数据库的操作
//创建一个表格,表格名为mytable,id自动增长,列数为三列,数据类型为varchar(字符串)
db.execSQL("CREATE TABLE mytable (_id INTEGER PRIMARY KEY AUTOINCREMENT, latitude varchar, longitude varchar, time varchar)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO 更改数据库版本的操作
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
// TODO 每次成功打开数据库后首先被执行
}
}
//插入
ContentValues values = new ContentValues();
values.put("latitude", strLatitude);
values.put("longitude", strLongitude);
values.put("time", time);
DatabaseHelper dbHelper = new DatabaseHelper(MyService.this, "mydata.db");
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert("mytable", null, values);
//查询
DatabaseHelper dbHelper = new DatabaseHelper(MyActivity.this, "mydata.db");
SQLiteDatabase db = dbHelper.getReadableDatabase();
//(表名, new String[] {要查询的列}, "_id=?", new String[] {要查询的id}, 后面的是一些分组排列方式,想要用的话可以查一下,本人没用到就全设为空值);
Cursor cursor = db.query("mytable", new String[] {"latitude", "longitude", "time"}, "_id=?", new String[] {id}, null, null, null);
while (cursor.moveToNext()) {
//查到后的操作,这里将输出显示在文本中
String latitudeStr = cursor.getString(cursor.getColumnIndex("latitude"));
String longitudeStr = cursor.getString(cursor.getColumnIndex("longitude"));
String timeStr = cursor.getString(cursor.getColumnIndex("time"));
txtQueryLongitude.setText(latitudeStr);
txtQueryLatitude.setText(longitudeStr);
txtQueryTime.setText(timeStr);
}
//查询全部数据
Cursor mCursor = mSQLiteDatabase.rawQuery("SELECT * FROM mytable", null);
cursor.moveToFirst(); //指向第一条数据
while (!(cursor.isLast())) { //数据不为空
String latitudeStr = cursor.getString(cursor.getColumnIndex("latitude"));
String longitudeStr = cursor.getString(cursor.getColumnIndex("longitude"));
String timeStr = cursor.getString(cursor.getColumnIndex("time"));
Map<String, String> map = new HashMap<String, String>();
map.put("latitude", latitudeStr);
map.put("longitude", longitudeStr);
map.put("time", timeStr);
list.add(map); //list这里未定义,List<Map<String, String>>
cursor.moveToNext(); //指向下一条数据
}
//修改
DatabaseHelper dbHelper = new DatabaseHelper(MyActivity.this, "mydata.db");
SQLiteDatabase db = dbHelper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", strName);
db.update("mytable", values, "id=?", new String[]{修改的id});
//删除
DatabaseHelper dbHelper = new DatabaseHelper(MyActivity.this, "mydata.db");
SQLiteDatabase db = dbHelper.getReadableDatabase();
db.delete("表名", "id=?", new String[]{1}); //删除了id为1的数据
db.delete(“表名”,“id? and no=?”,new String[]{1, 1});//删除了id为1且no为1的数据
本人发布的内容均为学习中用过的代码,上传主要为了方便以后的复习和为他人提供一些方便,一些代码没有上下文,新学的朋友有不懂或存在问题的地方可以留言。