package com.ljy;import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.content.ContentValues;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class mainActivity extends Activity {
    /** Called when the activity is first created. */

SQLiteDatabase db = null;
String dbnameString = "ljy.db";
String tablenameString = "table1";

String  id="_id";
String  name = "Name";
String  sex = "SEX";

Button bt1,bt2,bt3,bt4;
TextView tv1,tv2,tv3,tv4;

String creatTable = "CREATE TABLE"+tablenameString+"("+id+"INRERGER PRIMARY KEY"+","
+name+"TEXT"+","+sex+"INTERGER)";
int i=0;
protected String tag="AAA";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        bt1=(Button)findViewById(R.id.bt1);
        bt2=(Button)findViewById(R.id.bt2);
        bt3=(Button)findViewById(R.id.bt3);
        bt4=(Button)findViewById(R.id.bt4);
        tv1=(TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        tv3=(TextView)findViewById(R.id.tv3);
        tv4=(TextView)findViewById(R.id.tv4);
        
        db=this.openOrCreateDatabase(dbnameString, MODE_PRIVATE, null);
        try{
        db.execSQL(creatTable);
        }catch (Exception e) {
// TODO: handle exception
         e.printStackTrace();
}
        
        bt1.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub

ContentValues cv = new ContentValues();
cv.put(name, "罗建英");
cv.put(sex, 23);
db.insert(tablenameString, null, cv);
i++;
tv1.setText("插入成功"+i);
Log.v(tag, "1");
}
});
        bt2.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub

String delete = "DELETE FROM"+tablenameString+"WHERE _id =1";
try{
db.execSQL(delete);
tv2.setText("删除第一项");//+num);
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
        bt3.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub

ContentValues cv = new ContentValues();
cv.put(name, "熙熙");
cv.put(sex, 23);
int num=db.update(tablenameString, cv, name+"=?", new String[]{"罗建英"});
tv3.setText("更新第三项"+num);
}
});
        bt4.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
String select = "SELECT FROM"+tablenameString+"WHERE _id = 5";
try{
db.execSQL(select);
tv4.setText("查找第五项");
Log.v(tag, "42");
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
        
        
    }
}