App.java(初始化数据库)
package com.example.testsqlite;
import com.example.db.DbHelper;
import android.app.Application;
import android.content.Context;
public class App extends Application {
static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
DbHelper dbHelper = DbHelper.getInstance(this);
dbHelper.getWritableDatabase();//启动程序的时候会检测有没有这个数据库,如果没有就会执行onCreate();
}
public static Context getContext() {
return mContext;
}
}
dbHelper.java(创建表,更新版本)
package com.example.db;
import com.example.testsqlite.App;
import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DbHelper extends sqliteOpenHelper{
private String TAG = "DbHelper";
public static DbHelper instance;
private final static String DB_NAME = "Test.db";
private final static int VERSION = 2;//版本
private String CREATE_TEST = "CREATE TABLE TEST (Id text Not NULL,Name text,Age int,PRIMARY KEY(\"Id\") )";
private String CREATE_TEST2 = "CREATE TABLE TEST2 (Id text,PRIMARY KEY(\"Id\") )";
public DbHelper(Context context) {
super(context,DB_NAME,null,VERSION);
}
@Override
public void onCreate(sqliteDatabase db) {
db.execsql(CREATE_TEST);
db.execsql(CREATE_TEST2);
Toast.makeText(App.getContext(),"create",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
switch (oldVersion) {
case 1:
db.execsql(CREATE_TEST2);
Toast.makeText(App.getContext(),"v2",Toast.LENGTH_LONG).show();
// break; //不需要break,为了能一直往上升级数据库版本
default:
}
}
public static DbHelper getInstance(Context context) {
if (instance == null) {
instance = new DbHelper(context);
}
return instance;
}
}
TestDao.java(增删改查)
package com.example.db;
import java.util.ArrayList;
import java.util.List;
import com.examole.bean.TestVm;
import com.example.testsqlite.App;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.provider.Telephony.Sms.Conversations;
import android.util.Log;
public class TestDao {
private String TAG = "TestDao";
private DbHelper dbHelper;
private sqliteDatabase db;
private String TABLE_NAME = "TEST";
public TestDao() {
dbHelper = DbHelper.getInstance(App.getContext());
db = dbHelper.getWritableDatabase();
}
private TestVm readTest(Cursor cr) {
TestVm testVm = new TestVm();
testVm.setId(cr.getString(cr.getColumnIndex("Id")));
testVm.setName(cr.getString(cr.getColumnIndex("Name")));
testVm.setAge(cr.getInt(cr.getColumnIndex("Age")));
return testVm;
}
/** * 增,改 * @param testVm * @return */
public boolean updateOrSave(TestVm testVm) {
ContentValues cv = new ContentValues();
cv.put("Id",testVm.getId());
cv.put("Name",testVm.getName());
cv.put("Age",testVm.getAge());
boolean flag = db.update(TABLE_NAME,cv,"Id = ?",new String[]{ testVm.getId() }) > 0;//update失败的话 返回值是 0
if (flag) {
return true;
} else {
return db.insert(TABLE_NAME,cv) != -1;
}
}
/** * 查 * @return */
public List<TestVm> getAll() {
List<TestVm> list = new ArrayList<TestVm>();
Cursor cr = db.rawQuery("select * from "+TABLE_NAME,null);
while (cr.moveToNext()) {
list.add(readTest(cr));
}
return list;
}
/** * 删 * @param testVm */
public void delete(TestVm testVm) {
db.delete(TABLE_NAME,"id = ?",new String[]{ testVm.getId() });
}
/** * 清空 */
public void clear() {
db.execsql("delete from "+TABLE_NAME);
}
}
TestVm.java(Bean文件)
package com.examole.bean;
public class TestVm {
private String Id;
private String Name;
private int Age;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
}
MainActivity.java
package com.example.testsqlite;
import java.util.List;
import com.examole.bean.TestVm;
import com.example.db.TestDao;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText ed1;
Button bt1;
Button bt2;
Button bt3;
Button bt4;
TestDao dao;
String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dao = new TestDao();
ed1 = (EditText) findViewById(R.id.ed_id);
bt1 = (Button) findViewById(R.id.bt_updateorsave);
bt1.setOnClickListener(this);
bt2 = (Button) findViewById(R.id.bt_getall);
bt2.setOnClickListener(this);
bt3 = (Button) findViewById(R.id.bt_delete);
bt3.setOnClickListener(this);
bt4 = (Button) findViewById(R.id.bt_clear);
bt4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_updateorsave:
TestVm testVm = new TestVm();
testVm.setId("222301");
testVm.setName("tom");
testVm.setAge(21);
dao.updateOrSave(testVm);
break;
case R.id.bt_getall :
List<TestVm> list = dao.getAll();
String all = "";
for (TestVm testVm2 : list) {
all += testVm2.getId()+",";
all += testVm2.getName()+",";
all += testVm2.getAge()+" \n";
}
Log.v(TAG,all);
break;
case R.id.bt_delete :
String id = ed1.getText().toString();
TestVm testVm3 = new TestVm();
testVm3.setId(id);
dao.delete(testVm3);
break;
case R.id.bt_clear :
dao.clear();
break;
}
}
}
界面