我正在尝试使用sqlite在eclipse中从数据库中插入和检索数据,但是它显示了RuntimeError.我创建了一个包含三个编辑文本和一个按钮的布局,用于从中创建简单信息,但是什么也没有创建.我使用以下代码创建Java数据库:
package com.example.databasetest;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlException;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;
public class DBHelper extends sqliteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "teacher";
private static final String TABLE_NAME = "teacher_table";
private static final String NAME = "teacher_name";
private static final String FATHER_NAME = "father_name";
private static final String MOTHER_NAME = "mother_name";
sqliteDatabase data=this.getWritableDatabase();
Context ctx;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx=context;
Log.d("DATABASE OPERATION", "DATABASE CREATED");
}
@Override
public void onCreate(sqliteDatabase db) {
data=db;
db.execsql("CREATE TABLE " + TABLE_NAME + " ("
+ NAME + " TEXT,"
+ FATHER_NAME + " TEXT,"
+ MOTHER_NAME + " INTEGER,"
+ ");");
Log.d("DATABASE OPERATION", "TABLE CREATED");
}
public void open() throws sqlException
{
DBHelper db1 = new DBHelper(ctx);
data = db1.getWritableDatabase();
}
public void close()
{
data.close();
}
@Override
public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {
db.execsql("DROP TABLE IF EXCEPTION EXISTS");
onCreate(db);
}
public void onInsert(DBHelper db,String name,String f_name, String m_name)
{
sqliteDatabase sql= db.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("teacher_name",name);
cv.put("father_name", f_name);
cv.put("mother_name", m_name);
sql.insert(TABLE_NAME, null, cv);
Log.d("DATABASE OPERATION", "ONE ROW INSERTED.....");
}
}
和java文件为…
package com.example.databasetest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText NAME,FATHER,MOTHER,ID;
String name,father,mother,id;
int i=1;
Button save;
Context ctxx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NAME=(EditText)findViewById(R.id.name);
FATHER=(EditText)findViewById(R.id.father_name);
MOTHER=(EditText)findViewById(R.id.mother_name);
ID=(EditText)findViewById(R.id.emp_id);
save=(Button)findViewById(R.id.submit);
save.setonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id=ID.getText().toString();
name=NAME.getText().toString();
father=FATHER.getText().toString();
mother=MOTHER.getText().toString();
if(id.equals(i))
{
Toast.makeText(getBaseContext(), "not allow insertion", Toast.LENGTH_LONG).show();
}
else
{
DBHelper DB=new DBHelper(ctxx);
DB.open();
DB.onInsert(DB, name, father, mother);
Toast.makeText(getBaseContext(), "insertion sucessful", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
05-08 02:54:05.932: D/AndroidRuntime(922): Shutting down VM
05-08 02:54:05.999: W/dalvikvm(922): threadid=1: thread exiting with uncaught exception (group=0x41465700)
05-08 02:54:06.189: E/AndroidRuntime(922): FATAL EXCEPTION: main
05-08 02:54:06.189: E/AndroidRuntime(922): java.lang.NullPointerException
05-08 02:54:06.189: E/AndroidRuntime(922): at android.database.sqlite.sqliteOpenHelper.getDatabaseLocked(sqliteOpenHelper.java:224)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.database.sqlite.sqliteOpenHelper.getWritableDatabase(sqliteOpenHelper.java:164)
05-08 02:54:06.189: E/AndroidRuntime(922): at com.example.databasetest.DBHelper.<init>(DBHelper.java:22)
05-08 02:54:06.189: E/AndroidRuntime(922): at com.example.databasetest.MainActivity$1.onClick(MainActivity.java:49)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.view.View.performClick(View.java:4240)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.view.View$PerformClick.run(View.java:17721)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.os.Handler.handleCallback(Handler.java:730)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.os.Looper.loop(Looper.java:137)
05-08 02:54:06.189: E/AndroidRuntime(922): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-08 02:54:06.189: E/AndroidRuntime(922): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 02:54:06.189: E/AndroidRuntime(922): at java.lang.reflect.Method.invoke(Method.java:525)
05-08 02:54:06.189: E/AndroidRuntime(922): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-08 02:54:06.189: E/AndroidRuntime(922): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-08 02:54:06.189: E/AndroidRuntime(922): at dalvik.system.NativeStart.main(Native Method)
05-08 02:54:09.589: I/Process(922): Sending signal. PID: 922 SIG: 9
解决方法:
ctxx不会被初始化,这可能是崩溃的原因.一般来说,当您处理Activity和Fragment子类时,几乎不需要保留对Context的引用.活动是上下文的子类,通常就足够了.在Fragment中,您可以使用getActivity()检索托管Fragment的Activity的上下文
CHANTE
DBHelper DB=new DBHelper(ctxx);
同
DBHelper DB=new DBHelper(MainActivity.this);
正如@DerGolem所指出的,您正在将INTEGER类型用于MOTHER_NAME列.可能您想使用TEXT,并且还需要一个主键“ _id”
db.execsql("CREATE TABLE " + TABLE_NAME + " ("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME + " TEXT,"
+ FATHER_NAME + " TEXT,"
+ MOTHER_NAME + " TEXT"
+ ");");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。