我想创建数据库,我有这个错误
package it.unimi.di.simplephone;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBhelper extends SQLiteOpenHelper
{
public static final String DBNAME="BILLBOOK";
public DBhelper(Context context) {
super(context, DBNAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db= SQLiteDatabase.openDatabase("Log", null, Context.MODE_PRIVATE);
String q="CREATE TABLE "+DatabaseStrings.TBL_NAME+
" ( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
DatabaseStrings.FIELD_NOME+" TEXT," +
DatabaseStrings.FIELD_NUMERO+" TEXT)";
db.execSQL(q);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{ }
}
这是我的DBhelper课程。
这是我的活动课
package it.unimi.di.simplephone;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ListActivity{
private ListAdapter mAdapter = null;
//database
private DBhelper dbhelper;
private Context ctx;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//DATABASE
dbhelper=new DBhelper(ctx);
dbhelper.onCreate(db);
Cursor c = getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
String[] columns = new String[] { CommonDataKinds.Phone.DISPLAY_NAME };
int[] names = new int[] { R.id.contact_name };
mAdapter = new SimpleCursorAdapter(this, R.layout.row_layout, c,
columns, names,
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(mAdapter);
Button button = (Button) findViewById(R.id.BtnComponi);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent it=new Intent(MainActivity.this,componi.class);
startActivity(it);
}
});
Button log = (Button) findViewById(R.id.LastCall);
log.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent itlog=new Intent(MainActivity.this,log.class);
startActivity(itlog);
}
});
}
@Override
protected void onStart() {
super .onStart();
Cursor c = getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
String[] columns = new String[] { CommonDataKinds.Phone.DISPLAY_NAME };
int[] names = new int[] { R.id.contact_name };
mAdapter = new SimpleCursorAdapter(this, R.layout.row_layout, c,
columns, names,
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = (Cursor) mAdapter.getItem(position);
String phone = c.getString(c
.getColumnIndex(CommonDataKinds.Phone.NUMBER));
//database
String nome= c.getString(c.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
//database
db=dbhelper.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("nome", nome);
cv.put("numero", phone);
try
{
db.insert(DatabaseStrings.TBL_NAME, null,cv);
}
catch (SQLiteException sqle)
{
}
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:" + phone));
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误是无法打开数据库..但是使用类助手中的函数opendatabase我创建数据库(如果不存在)..
感谢
答案 0 :(得分:0)
我注意到的一些事情:
dbhelper=new DBhelper(ctx);
dbhelper.onCreate(db);
您不应该致电onCreate()
课程的SQLiteOpenHelper
。 Android在第一次创建此数据库时调用此方法。此外,您的崩溃可能是NullPointerException
,因为您正在将null
对象传递给此方法;成员字段db
从未初始化。
@Override
public void onCreate(SQLiteDatabase db)
{
db= SQLiteDatabase.openDatabase("Log", null, Context.MODE_PRIVATE);
String q="CREATE TABLE "+DatabaseStrings.TBL_NAME+
" ( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
DatabaseStrings.FIELD_NOME+" TEXT," +
DatabaseStrings.FIELD_NUMERO+" TEXT)";
db.execSQL(q);
}
您已在此处打开第二个数据库文件,并为其添加了一个表格。但是你永远不会在当前数据库中创建一个表。这是故意的吗?如果没有,您应该使用在执行SQL时传递给您的db
参数,因为那是SQLiteOpenHelper
为您打开的参数。
如果第二个数据库文件是故意的,那么值得注意的是,因为你没有SQLiteOpenHelper
来记录" Log"数据库,您只能使用SQLiteDatabase.openDatabase
打开它,并且不会自动获取onCreate
和onUpgrade
个钩子。如果您只需要一个表,那么我可能会建议您在主数据库中使用该表。