错误数据库SQLite的无法打开数据库 [英] Error Database Sqlite Could not open database
问题描述
我triyng创建数据库和我有这个错误
I triyng to create database and i have this error
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类..
this is my DBhelper class..
这是我的活动类
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创建数据库,如果不存在。
the error is Could not open database.. but with the function opendatabase in Class helper i create database if not exist..
感谢
推荐答案
有几件事情我已经注意到了:
A few things I've noticed:
dbhelper=new DBhelper(ctx);
dbhelper.onCreate(db);
您不应该调用的onCreate()
你的 SQLiteOpenHelper
类的。这是通过所谓的Android正在创建该数据库的第一次。此外,你的崩溃可能是一个 NullPointerException异常
,因为你是传递一个空
对象来此方法;成员字段分贝
从未被初始化。
You should not be calling onCreate()
of your SQLiteOpenHelper
class. This is called by Android the first time this database is being created. Additionally, your crash is probably a NullPointerException
because you are passing a null
object to this method; the member field db
was never initialized.
@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时传递给你的分贝
的说法,因为这是一个 SQLiteOpenHelper
是开放给你。
You've opened a second database file here and added a table to that. But you never create a table in the current database. Is this intentional? If not, you should use the db
argument that was passed to you when executing your SQL, because that's the one that SQLiteOpenHelper
is opening for you.
如果第二个数据库文件是故意的,这是值得注意的,因为你没有 SQLiteOpenHelper
为日志的数据库,你永远只能用<$ C打开$ C> SQLiteDatabase.openDatabase ,你不自动获得的onCreate
和 onUpgrade
挂钩。如果你需要的是一个表有,那么我可能会建议在你的主数据库中的表来代替。
If the second database file IS intentional, it's worth noting that because you have no SQLiteOpenHelper
for the "Log" database, you can only ever open it with SQLiteDatabase.openDatabase
and you do not automatically get onCreate
and onUpgrade
hooks. If all you need is one table there, then I might suggest having that table in your main database instead.
这篇关于错误数据库SQLite的无法打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!