1,用execSQL语句怎么把edittext的内容插入到表中呢?2,为什么输入关闭数据库的语句Logcat就会出错?
package com.example.lianxi;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
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.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText e1,e2,e3;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.e1);
e2=(EditText)findViewById(R.id.e2);
e3=(EditText)findViewById(R.id.e3);
b1=(Button)findViewById(R.id.insert);
final SQLiteDatabase db=openOrCreateDatabase("user2.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists usertb2(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String a=e1.getText().toString();
String b=e2.getText().toString();
int c=Integer.parseInt(e3.getText().toString());
ContentValues values=new ContentValues();
values.put("age", c);
values.put("sex", b);
values.put("name", a);
db.insert("usertb2", null, values);
//db.execSQL("insert into userdb1(name,sex,age)values(a,b,11)");
//1,用execSQL语句怎么把edittext的内容插入到表中呢?
values.clear();
}
});
db.close();//2,为什么输入关闭数据库的语句Logcat就会出错?
}
}