在Android中连接数据库
我正在尝试创建一个数据库并将值插入表中。但这段代码不起作用,任何人都可以帮助我。提前致谢。
package com.bookshop;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.AutoCompleteTextView;
import android.database.sqlite.SQLiteDatabase;
public class BookShop extends Activity {
private AutoCompleteTextView txttitle;
private AutoCompleteTextView txtauthor;
private AutoCompleteTextView txtisbn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
txttitle = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewtitle);
txtauthor = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewauthor);
txtisbn = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewisbn);
}
public void onsubmit(View view) {
//txttitle.setText(ValueOf(txtauthor)+ ValueOf(txtisbn));
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Tittle : " + "" +(txttitle).getText()+"\n"+ "Author : "+"" + (txtauthor).getText()+"\n" + "ISBN : " +""+ (txtisbn).getText());
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
// show it
alertbox.show();
databaseacess();
}
public void databaseacess()
{
SQLiteDatabase db;
db = openOrCreateDatabase(
"BookShop.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
final String CREATE_TABLE_BookShop =
"CREATE TABLE bookshop ("
//+"id, INTEGER PRIMARY KEY AUTOINCREMENT"
+"title TEXT"
+"author TEXT"
+"isbn);";
db.execSQL(CREATE_TABLE_BookShop);
ContentValues values = new ContentValues();
values.put("title",(txttitle).getText().toString());
values.put("author",(txtauthor).getText().toString());
values.put("isbn",(txtisbn).getText().toString());
try {
db.insertOrThrow("bookshop", null, values );
} catch (Exception e) {
//catch code
}
}
public void onclear(View view) {
txttitle.setText("");
txtauthor.setText("");
txtisbn.setText("");
}
}
I am trying to create a database and inserting values into table. but this code is not working can any one help me.thanks in advance.
package com.bookshop;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.AutoCompleteTextView;
import android.database.sqlite.SQLiteDatabase;
public class BookShop extends Activity {
private AutoCompleteTextView txttitle;
private AutoCompleteTextView txtauthor;
private AutoCompleteTextView txtisbn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
txttitle = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewtitle);
txtauthor = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewauthor);
txtisbn = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewisbn);
}
public void onsubmit(View view) {
//txttitle.setText(ValueOf(txtauthor)+ ValueOf(txtisbn));
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Tittle : " + "" +(txttitle).getText()+"\n"+ "Author : "+"" + (txtauthor).getText()+"\n" + "ISBN : " +""+ (txtisbn).getText());
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
// show it
alertbox.show();
databaseacess();
}
public void databaseacess()
{
SQLiteDatabase db;
db = openOrCreateDatabase(
"BookShop.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
final String CREATE_TABLE_BookShop =
"CREATE TABLE bookshop ("
//+"id, INTEGER PRIMARY KEY AUTOINCREMENT"
+"title TEXT"
+"author TEXT"
+"isbn);";
db.execSQL(CREATE_TABLE_BookShop);
ContentValues values = new ContentValues();
values.put("title",(txttitle).getText().toString());
values.put("author",(txtauthor).getText().toString());
values.put("isbn",(txtisbn).getText().toString());
try {
db.insertOrThrow("bookshop", null, values );
} catch (Exception e) {
//catch code
}
}
public void onclear(View view) {
txttitle.setText("");
txtauthor.setText("");
txtisbn.setText("");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要学习如何在 Eclipse 中调试以及如何使用 ADB 和DDMS 工具。
为了获得有关异常/强制关闭的更多详细信息,您需要在 Eclipse 中查找名为 Logcat(您将在 DDMS< /a> 透视图)在那里您将找到问题出现的时间/内容以及哪一行的详细回溯。
为此,您应该阅读关于 使用 Eclipse 在 Android 中进行调试的完整文章
(来源:droidnova.com< /a>)
You need to learn how to Debug in Eclipse and how to use the ADB and DDMS tools.
In order to get more details about an exception/force close you need to look for a view in Eclipse called Logcat(you will find in the DDMS perspective) there you will find a detailed traceback when/what and on what line is the issue.
For this you should read a complete article about Debugging in Android using Eclipse
(source: droidnova.com)