首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

SQLiteOpenHelper创建数据库失败

2012-04-11 
求助:SQLiteOpenHelper创建数据库失败import android.content.Contextimport android.database.sqlite.SQ

求助:SQLiteOpenHelper创建数据库失败
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper {

private static final int VERSION = 1;
private static final String DBNAME= "dataTest.db";
final String CREATE_TABLE_MUSIC = "create table t_music (musicId integer primary key, musicName varchar(20), musicPath varchar(50) ";

public DBOpenHelper(Context context){
super(context, DBNAME, null, VERSION);
Log.d("数据库", "成功启动");
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_MUSIC);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}


package android.music;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.music.R;
import android.music.sqlite.DBOpenHelper;
import android.os.Bundle;
import android.util.Log;

public class MusicPlay extends Activity implements OnClickListener{

Intent serviceIntent;
DBOpenHelper helper;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/my.db", null );

helper = new DBOpenHelper(this);
Log.d("helper", "数据库new成功");
  }
}


在LogCat上有显示Log信息,但是在data中并没有找到DBOpenHelper类创建的数据,但是有SQLiteDatabase创建的数据,请教大侠为什么?谢谢

[解决办法]
调用help的getWritableDatabase和getReadableDatabase方法才会执行onCreate方法
db = help.getWritableDatabase();

热点排行