我是新来的Android以下是我的code我想在数据库中添加按钮的点击数据,但是当我按一下按钮我的应用程序崩溃 可有人建议我任何解决方案 这里是我的code:
公共类三延伸活动实现OnClickListener {
按钮BTN;
SQLiteDatabase分贝;
的EditText EDIT1;
的EditText EDIT2;
的EditText EDIT3;
@覆盖
保护无效的onCreate(包冰柱){
// TODO自动生成方法存根
super.onCreate(冰柱);
的setContentView(R.layout.mit);
SQLiteDatabase分贝;
DB = openOrCreateDatabase(doctorinfo.db,SQLiteDatabase.CREATE_IF_NECESSARY,NULL);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(真正的);
最后弦乐CREATE_TABLE_COUNTRIES =CREATE TABLE med_d(+号INTEGER PRIMARY KEY AUTOINCREMENT,+DOC_NAME文本,+DOC_NO文本,+DOC_EMAIL TEXT);;
//db.execSQL(CREATE_TABLE_COUNTRIES);
BTN =(按钮)findViewById(R.id.button1);
EDIT1 =(EditText上)findViewById(R.id.editText1);
EDIT2 =(EditText上)findViewById(R.id.editText2);
EDIT3 =(EditText上)findViewById(R.id.editText3);
btn.setOnClickListener(本);
}
@覆盖
公共无效的onClick(查看为arg0){
串D = edit1.getText()的toString()。
。串M = edit2.getText()的toString();
。串P = edit3.getText()的toString();
ContentValues值=新ContentValues();
values.put(DOC_NAME,D);
values.put(DOC_NO,米);
values.put(DOC_EMAIL,p)的;
//长countryId = db.insert(med_d,空,价值观);
//尝试 {
db.insertOrThrow(med_d,空,价值观);
} //赶上(例外五){
//Log.e("Add错误,e.toString());
//e.printStackTrace();
//}
}
解决方案
SQLITEOPENHELPER CLASS
包com.db.demo;
进口android.content.Context;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.provider.BaseColumns;
/ **
*在{@link SQLiteOpenHelper}的子类,建立数据库的
*演示。
*
* @author尕尔
* /
公共类DatabaseHelper扩展SQLiteOpenHelper {
公共DatabaseHelper(上下文的背景下){
超级(上下文中,CursorDemo,空,1);
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(CREATE TABLE IF NOT EXISTS名(
+ BaseColumns._ID
+INTEGER PRIMARY KEY AUTOINCREMENT,第一VARCHAR,最后VARCHAR));
db.execSQL(INSERT INTO名(第一,最后)VALUES('约翰','李四'));
db.execSQL(INSERT INTO名(第一,最后)VALUES('詹姆斯','柯克'));
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
//步骤升级数据库新版本...
}
}
*的的活动类的*
包com.db.demo;
进口android.app.ListActivity;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.provider.BaseColumns;
进口android.widget.CursorAdapter;
进口android.widget.ListView;
进口android.widget.SimpleCursorAdapter;
公共类DataHandlingActivity扩展ListActivity {
私人SQLiteDatabase数据库;
字符串字段[] = {第一,最后,BaseColumns._ID};
私人CursorAdapter的数据源;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
DatabaseHelper帮手=新DatabaseHelper(本);
数据库= helper.getReadableDatabase();
光标数据= database.query(名,字段,NULL,NULL,NULL,NULL,
空值);
数据源=新SimpleCursorAdapter(这一点,R.layout.row,数据字段,
新的INT [] {R.id.first,R.id.last});
ListView的视图= getListView();
view.setHeaderDividersEnabled(真正的);
view.addHeaderView(getLayoutInflater()膨胀(R.layout.row,NULL));
setListAdapter(数据源);
}
}
使用像这样的按钮,点击和更改活动课 update SQL数据库ContentValues和更新,方法
I am new to Android following is my code I wants to add data in data base on the click of button but when I click on the Button my application crashes can anybody suggest me any solution here is my code:
public class Third extends Activity implements OnClickListener{
Button btn;
SQLiteDatabase db;
EditText edit1;
EditText edit2;
EditText edit3;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.mit);
SQLiteDatabase db;
db = openOrCreateDatabase("doctorinfo.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
final String CREATE_TABLE_COUNTRIES ="CREATE TABLE med_d ("+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"+ "DOC_NAME TEXT,"+ "DOC_NO TEXT,"+ "DOC_EMAIL TEXT);";
//db.execSQL(CREATE_TABLE_COUNTRIES);
btn=(Button)findViewById(R.id.button1);
edit1=(EditText)findViewById(R.id.editText1);
edit2=(EditText)findViewById(R.id.editText2);
edit3=(EditText)findViewById(R.id.editText3);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
String d=edit1.getText().toString();
String m=edit2.getText().toString();
String p=edit3.getText().toString();
ContentValues values = new ContentValues();
values.put("DOC_NAME",d );
values.put("DOC_NO",m);
values.put("DOC_EMAIL",p);
//long countryId = db.insert("med_d", null, values);
//try {
db.insertOrThrow("med_d", null,values);
}// catch (Exception e) {
//Log.e("Add Error", e.toString());
//e.printStackTrace();
//}
}
解决方案
SQLITEOPENHELPER CLASS
package com.db.demo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
/**
* Subclass of the {@link SQLiteOpenHelper} that sets up the database for the
* demo.
*
* @author Kah
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "CursorDemo", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS names ("
+ BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, first VARCHAR, last VARCHAR)");
db.execSQL("INSERT INTO names (first, last) VALUES ('John', 'Doe')");
db.execSQL("INSERT INTO names (first, last) VALUES ('James', 'Kirk')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Steps to upgrade the database for the new version ...
}
}
*Activity class*
package com.db.demo;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class DataHandlingActivity extends ListActivity{
private SQLiteDatabase database;
String fields[] = { "first", "last", BaseColumns._ID };
private CursorAdapter dataSource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseHelper helper = new DatabaseHelper(this);
database = helper.getReadableDatabase();
Cursor data = database.query("names", fields, null, null, null, null,
null);
dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields,
new int[] { R.id.first, R.id.last });
ListView view = getListView();
view.setHeaderDividersEnabled(true);
view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));
setListAdapter(dataSource);
}
}
use something like this in button click and change activity class update sql database with ContentValues and the update-method