|
|
|
@ -0,0 +1,134 @@
|
|
|
|
|
package com.example.myapplication;
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
public class SqliteDbManager
|
|
|
|
|
{
|
|
|
|
|
private static SqliteDbManager mInstance = null;
|
|
|
|
|
private SQLiteDatabase mDb = null;
|
|
|
|
|
private PersonSQLiteOpenHelper mDbHelper = null;
|
|
|
|
|
|
|
|
|
|
public static SqliteDbManager getInstance()
|
|
|
|
|
{
|
|
|
|
|
if (mInstance == null)
|
|
|
|
|
{
|
|
|
|
|
mInstance = new SqliteDbManager();
|
|
|
|
|
}
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSqliteDbOpen(Context context)
|
|
|
|
|
{
|
|
|
|
|
mDbHelper = new PersonSQLiteOpenHelper(context.getApplicationContext());
|
|
|
|
|
mDb = mDbHelper.getWritableDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void insertTb(String tbName, String username, String password)
|
|
|
|
|
{
|
|
|
|
|
openDb();
|
|
|
|
|
//方法一:
|
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
|
contentValues.put("username",username);
|
|
|
|
|
contentValues.put("password",password);
|
|
|
|
|
// contentValues.put("sex","男");
|
|
|
|
|
mDb.insert(tbName,null,contentValues);
|
|
|
|
|
//方法二:
|
|
|
|
|
// mDb.execSQL("insert into "+tbName+" (username,password) values ('李四',20)");
|
|
|
|
|
// mDb.execSQL("insert into "+tbName+" (name,age,sex) values ('王五',22,'女')");
|
|
|
|
|
// mDb.execSQL("insert into "+tbName+" (name,age,sex) values ('哈利',21,'男')");
|
|
|
|
|
closeDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteTb(String tbName)
|
|
|
|
|
{
|
|
|
|
|
openDb();
|
|
|
|
|
//方法一:
|
|
|
|
|
mDb.delete(tbName,"name=?",new String[]{"zhangsan"});
|
|
|
|
|
//方法二:
|
|
|
|
|
mDb.execSQL("delete from "+tbName+" where name = '李四'");
|
|
|
|
|
closeDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateTb(String tbName)
|
|
|
|
|
{
|
|
|
|
|
openDb();
|
|
|
|
|
//方法一:
|
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
|
contentValues.put("name","隔壁老王");
|
|
|
|
|
contentValues.put("sex","男");
|
|
|
|
|
mDb.update(tbName,contentValues,"name=?",new String[]{"王五"});
|
|
|
|
|
//方法二:
|
|
|
|
|
mDb.execSQL("update "+tbName+" set name = '哈利波特',age = '16' where name = '哈利'");
|
|
|
|
|
closeDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean queryTb(String tbName, String username, String password)
|
|
|
|
|
{
|
|
|
|
|
openDb();
|
|
|
|
|
// //方法一:
|
|
|
|
|
// Cursor cursor = mDb.query(tbName, new String[]{"name","sex","age"}, "name=?", new String[]{"隔壁老王"}, null, null, null);
|
|
|
|
|
// //将光标移动到下一行,从而判断该结果集是否还有下一条数据;如果有则返回true,没有则返回false
|
|
|
|
|
// if (null != cursor)
|
|
|
|
|
// {
|
|
|
|
|
// while (cursor.moveToNext())
|
|
|
|
|
// {
|
|
|
|
|
// String name = cursor.getString(cursor.getColumnIndex("name"));
|
|
|
|
|
// int age = cursor.getInt(cursor.getColumnIndex("age"));
|
|
|
|
|
// String sex = cursor.getString(cursor.getColumnIndex("sex"));
|
|
|
|
|
// Log.i("????","name = "+name+"; age = "+age+"; sex = "+sex);
|
|
|
|
|
// }
|
|
|
|
|
// cursor.close();
|
|
|
|
|
// }
|
|
|
|
|
//方法二:使用sql语句
|
|
|
|
|
Cursor rawQuery = mDb.rawQuery("select * from " + tbName+" where username=? and password=?", new String[]{username,password});
|
|
|
|
|
// Cursor rawQuery = mDb.rawQuery("select * from " + tbName,null);
|
|
|
|
|
if (null != rawQuery)
|
|
|
|
|
{
|
|
|
|
|
while (rawQuery.moveToNext())
|
|
|
|
|
{
|
|
|
|
|
@SuppressLint("Range") String _id = rawQuery.getString(rawQuery.getColumnIndex("id"));
|
|
|
|
|
@SuppressLint("Range") String name = rawQuery.getString(rawQuery.getColumnIndex("username"));
|
|
|
|
|
// int age = rawQuery.getInt(rawQuery.getColumnIndex("age"));
|
|
|
|
|
// String sex = rawQuery.getString(rawQuery.getColumnIndex("sex"));
|
|
|
|
|
Log.i("????","_id = "+_id+"; name = "+name);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
rawQuery.close();
|
|
|
|
|
}
|
|
|
|
|
closeDb();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建或打开一个可以读的数据库
|
|
|
|
|
*/
|
|
|
|
|
private void openDb() {
|
|
|
|
|
if (this.mDbHelper != null) {
|
|
|
|
|
try {
|
|
|
|
|
mDb = mDbHelper.getWritableDatabase();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
mDb = mDbHelper.getReadableDatabase();
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关闭数据库
|
|
|
|
|
*/
|
|
|
|
|
private void closeDb() {
|
|
|
|
|
try {
|
|
|
|
|
if (mDb != null) {
|
|
|
|
|
mDb.close();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|