//创建数据库,增、删、改、查,排序
public class dbHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME="dxt_db";
private final static int DATABASE_VERSION=2;
private final static String TABLE_NAME="dong_info";
public final static String FIELD_ID="_id";
public final static String FIELD_NAME="Name";
public final static String FIELD_TABLE_NUMBER="Table_Number";
public final static String FIELD_INFO="Info";
public final static String FIELD_TITLE="gender";
public dbHelper(Context context)
{
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// String sql="Create table "+TABLE_NAME+"("+FIELD_ID+" integer primary key autoincrement,"
// +FIELD_TITLE+" text );";
String sql="CREAT TABLE IF NOT EXISTS"+TABLE_NAME+"("+FIELD_ID+" integer primary key autoincrement,"
+FIELD_NAME+" text, "+FIELD_TABLE_NUMBER+" text, "+FIELD_INFO+" text, "
+FIELD_TITLE+" text );";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public Cursor select()
{
SQLiteDatabase db=this.getReadableDatabase();
String[] column = {FIELD_ID,FIELD_NAME,FIELD_TABLE_NUMBER,FIELD_INFO,"sec_Title"};
// Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, " _id asc");
// Cursor cursor=db.query(TABLE_NAME, column, null, null, null, null, " _id desc");
Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, " _id desc");
// Log.d("tag", "-----"+cursor.getString(0));
Log.d("tag", "数据库长度--"+cursor.getCount());
Log.d("tag", "数据库列名称--"+cursor.getColumnName(0));
Log.d("tag", "数据库列数--"+cursor.getColumnCount());
Log.d("tag", "数据库名称是第几列--"+cursor.getColumnIndexOrThrow("_id"));
Log.d("tag", "数据库位置---|"+cursor.getPosition());
cursor.moveToFirst();
while (cursor.getPosition() != cursor.getCount()) {
Log.d("tag", "数据库内容-->"+cursor.getString(1));
cursor.moveToNext();
}
//最后的参数 desc是descend 降序意思 asc 是ascend 升序的意思
/**
* 关于db.query方法的参数,有很多,为了防止大家弄乱,我简单说一下
参数1:表名
参数2:返回数据包含的列信息,String数组里放的都是列名
参数3:相当于sql里的where,sql里where后写的内容放到这就行了,例如:tage>?
参数4:如果你在参数3里写了?(知道我为什么写tage>?了吧),
那个这里就是代替?的值 接上例:new String[]{"30"}
参数5:分组,不解释了,不想分组就传null
参数6:having,想不起来的看看SQL
参数7:orderBy排序
*/
return cursor;
}
public long insert(String Name,String TableNumber,String Info,String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(FIELD_NAME, Name);
cv.put(FIELD_TABLE_NUMBER, TableNumber);
cv.put(FIELD_INFO, Info);
cv.put(FIELD_TITLE, Title);
long row=db.insert(TABLE_NAME, null, cv);
return row;
}
public void delete(int id)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIELD_ID+"=?";
String[] whereValue={Integer.toString(id)};
db.delete(TABLE_NAME, where, whereValue);
}
public void update(int id,String Name,String TableNumber,String Info,String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIELD_ID+"=?";
String[] whereValue={Integer.toString(id)};
ContentValues cv=new ContentValues();
cv.put(FIELD_NAME, Name);
cv.put(FIELD_TABLE_NUMBER, TableNumber);
cv.put(FIELD_INFO, Info);
cv.put(FIELD_TITLE, Title);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
主程序,创建菜单 对数据库进行操作,其中用到字符串解析
public class testDbActivity extends Activity {
private dbHelper db;
private Cursor myCursor;
private ListView myListView;
private EditText myEditText;
private int _id;
protected final static int MENU_ADD=Menu.FIRST;
protected final static int MENU_EDIT=Menu.FIRST+1;
protected final static int MENU_DELETE=Menu.FIRST+2;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_ADD, 0, R.string.ADD);
menu.add(Menu.NONE, MENU_EDIT, 0,R.string.EDIT);
menu.add(Menu.NONE, MENU_DELETE, 0,R.string.DELETE);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_ADD:
operation("add");
break;
case MENU_EDIT:
operation("edit");
break;
case MENU_DELETE:
operation("delete");
break;
default:
break;
}
return true;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText=(EditText)findViewById(R.id.EditText1);
myListView=(ListView)findViewById(R.id.ListView1);
db=new dbHelper(testDbActivity.this);
myCursor=db.select();//数据库信息
SimpleCursorAdapter adpater=new SimpleCursorAdapter(this
, R.layout.test, myCursor,
new String[]{dbHelper.FIELD_NAME,dbHelper.FIELD_TABLE_NUMBER,dbHelper.FIELD_INFO,dbHelper.FIELD_TITLE},
new int[]{R.id.topTextView,R.id.name_view,R.id.tableNumber_view,R.id.info_view});
/**
* 参数1:Content
参数2:布局
参数3:Cursor游标对象
参数4:显示的字段,传入String[]
参数5:显示字段使用的组件,传入int[],该数组中是TextView组件的id
*/
myListView.setAdapter(adpater);
//点击的时候
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("tag", "arg0------>"+arg0);
Log.d("tag", "arg1------>"+arg1);
Log.d("tag", "arg2------>"+arg2);//在这个view上的位置
Log.d("tag", "arg3------>"+arg3);// The row id of the item that was clicked. 一直增加的id号
myCursor.moveToPosition(arg2);//选择的第几条信息
_id=myCursor.getInt(0);//此信息的第0项(ID号)
myEditText.setText(myCursor.getString(1));
}
});
//下面这段代码是在获取焦点时 显示焦点的内容
myListView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
SQLiteCursor sc=(SQLiteCursor)arg0.getSelectedItem();
_id=sc.getInt(0);
myEditText.setText(sc.getString(1));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private void operation(String cmd)
{
String et_str = myEditText.getText().toString();
if(et_str.equals("")){
Toast toast = Toast.makeText(getApplicationContext(), "请输入信息!!!!!!!", 0);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
String et_info[] = et_str.split(",");//解析字符串
for (int i = 0; i < et_info.length; i++) {
Log.d("tag", "-->"+et_info[i]);
}
if(cmd=="add"){
db.insert(et_info[0],et_info[1],et_info[2],et_info[3]);
}
if(cmd=="edit"){
db.update(_id, et_info[0],et_info[1],et_info[2],et_info[3]);
}
if(cmd=="delete"){
db.delete(_id);
}
myCursor.requery();//重新查询
myListView.invalidateViews();//更新listview
myEditText.setText("");
_id=0;
}
}
//主界面
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/AbsoluteLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/EditText1"
android:layout_width="292px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="11px"
android:layout_y="10px"
android:hint="name,table_number,info,title"
>
</EditText>
<ListView
android:id="@+id/ListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="12px"
android:layout_y="69px"
>
</ListView>
</AbsoluteLayout>
//listview显示的内容及布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="60px">
<TextView
android:text="TextView1" android:id="@+id/topTextView"
android:layout_width="wrap_content" android:textSize="20dip"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="@+id/name_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="->"></TextView>
<TextView
android:id="@+id/name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="TextView1"></TextView>
<TextView
android:id="@+id/name_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="->"></TextView>
<TextView
android:id="@+id/tableNumber_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="TextView1"></TextView>
<TextView
android:id="@+id/name_view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="->"></TextView>
<TextView
android:id="@+id/info_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="TextView1"></TextView>
</LinearLayout>