SQLite

package com.example.phonesqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class OpenHelper extends SQLiteOpenHelper{

String sql = "create table if not exists TestUsers"+"(id int primary key,name varchar,sex varchar)";

public OpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}

 

 

 

 

package com.example.phonesqlite;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity {

Button btnInsert;
Button btnDelete;
Button btnUpdate;
Button btnSelect;
EditText etName;
EditText etSex;
TextView tvShowContent;
OpenHelper openHelper;
SQLiteDatabase db=null;
public static final String DB_NAME = "DBTest";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

openHelper = new OpenHelper(this, DB_NAME, null, 1);

btnInsert=(Button)findViewById(R.id.btn_add);
btnInsert.setOnClickListener(btnInsertListener);

btnDelete=(Button)findViewById(R.id.btn_delete);
btnDelete.setOnClickListener(btnDeleteListener);

btnUpdate=(Button)findViewById(R.id.btn_update);
btnUpdate.setOnClickListener(btnUpdateListener);

btnSelect=(Button)findViewById(R.id.btn_select);
btnSelect.setOnClickListener(btnSelectListener);


etName=(EditText)findViewById(R.id.et_name);
etSex=(EditText)findViewById(R.id.et_sex);
tvShowContent=(TextView)findViewById(R.id.tv_showContent);
}


View.OnClickListener btnInsertListener = new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
InsertTb();
}
};

public void InsertTb() {
// TODO Auto-generated method stub
int flag = -1;
db = openHelper.getWritableDatabase();
String strName = etName.getText().toString();
String strSex = etSex.getText().toString();
String sql = "insert into TestUsers(name,sex)values('"+strName+"','"+strSex+"')";
try {
db.execSQL(sql);
} catch (Exception e) {
// TODO: handle exception
Log.i("err", "insert failed");
flag = 0;
Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag==-1) {
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}

}



View.OnClickListener btnDeleteListener = new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DeleteTb();
}
};
public void DeleteTb() {
// TODO Auto-generated method stub
int flag = -1;
db = openHelper.getWritableDatabase();
String strName = etName.getText().toString();
String sql = "delete from TestUsers where name = '"+strName+"'";
try {
db.execSQL(sql);
} catch (Exception e) {
// TODO: handle exception
Log.i("err", "delete failed");
flag = 0;
Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag==-1) {
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
}
}



View.OnClickListener btnUpdateListener = new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
UpdateTb();
}
};
public void UpdateTb() {
// TODO Auto-generated method stub
int flag = -1;
db = openHelper.getWritableDatabase();
String strName = etName.getText().toString();
String strSex = etSex.getText().toString();
String sql = "Update TestUsers set sex = '" +strSex+ "' where name='"+strName+"'";
try {
db.execSQL(sql);
} catch (Exception e) {
// TODO: handle exception
Log.i("err", "Update failed");
flag = 0;
Toast.makeText(MainActivity.this, "更新失败", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag==-1) {
Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
}
}


View.OnClickListener btnSelectListener = new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SelectTb();
}
};
public void SelectTb() {
// TODO Auto-generated method stub
/*db = openHelper.getWritableDatabase();
String sql = "select sex from TestUsers where name = ?";
Cursor cursor = db.rawQuery(sql, new String[]{ etName.getText().toString()});//获取查询名字的游标
int count = cursor.getCount();//count为行数
String [] Sex = new String[count];
int i=0;
if (cursor.getCount()>0) {
int sexIndex = cursor.getColumnIndex("sex");
for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
Sex[i] = cursor.getString(sexIndex);
i++;
}
}
for (int j = 0; j < count; j++) {
tvShowContent.append(" ");
tvShowContent.append(Sex[count]);
}
cursor.close();
db.close();*/

db = openHelper.getWritableDatabase();
String sql = "select sex from TestUsers where name = ?";
Cursor cursor = db.rawQuery(sql, new String[]{ etName.getText().toString()});//获取查询名字的游标
if (cursor.moveToFirst()) {
String Sex = cursor.getString(cursor.getColumnIndex("sex"));
tvShowContent.append(" ");
tvShowContent.append(Sex);
Toast.makeText(MainActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
}
cursor.close();
db.close();
}

}

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />


<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete" />

<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/update" />

<Button
android:id="@+id/btn_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/select" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name" />

<EditText
android:id="@+id/et_name"
android:hint="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex" />

<EditText
android:id="@+id/et_sex"
android:hint="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_showContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_data" />

</LinearLayout>

 

posted @ 2017-02-16 08:55  音为  阅读(141)  评论(0编辑  收藏  举报