我试图让我的第一个Android应用程序。我听说,如果没有创建数据库的SQLiteOpenHelper.onCreate()方法的过程创建表。然而,以为我试图调试的onCreate()方法没有奏效,甚至。请看看下面的code和给我任何建议。任何帮助将AP preciated。
====================================== =========================== 公共类NameToPinyinActivity延伸活动{ DatabaseOpenHelper帮手= NULL; @覆盖 公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.nametopinyin); 按钮searchButton =(按钮)findViewById(R.id.search); searchButton.setOnClickListener(新ButtonClickListener()); 辅助=新DatabaseOpenHelper(NameToPinyinActivity.this); } ================================================== =============== 公共类DatabaseOpenHelper扩展SQLiteOpenHelper { / ** DB名称* / 私有静态最后弦乐DB_NAME =拼音; / ** CREATE TABLE SQL * / 私有静态最后弦乐CREATE_TABLE_SQL =CREATE TABLE UNI code_PINYIN +(ID INTEGER PRIMARY KEY AUTOINCREMENT, +UNI code文本NOT NULL,拼音文字NOT NULL); 公共DatabaseOpenHelper(上下文的背景下){ 超级(上下文,DB_NAME,空,1); } @覆盖 公共无效的onCreate(SQLiteDatabase DB){ db.beginTransaction(); 尝试 { db.execSQL(CREATE_TABLE_SQL); db.setTransactionSuccessful(); }赶上(例外五){ e.printStackTrace(); } 最后 { db.endTransaction(); } } ================================================== ===============解决方案
我也有过与 SQLiteOpenHelper 的麻烦。什么工作对我来说是存储成员变量
SQLiteDatabase分贝;在SQLiteOpenHelper的子类,并要求
DB = getWritableDatabase();
在构造函数中。
在回答这个问题还包括有用的信息:SQLiteOpenHelper不能打电话的onCreate?
我希望这有助于!
I am trying to make my first android app. I heard that the SQLiteOpenHelper.onCreate() method process to create tables if the database is not created. However, the onCreate() method did not work even thought I tried to debug. Please look at the code below and give me any suggestions. Any help will be appreciated.
================================================================= public class NameToPinyinActivity extends Activity { DatabaseOpenHelper helper = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nametopinyin); Button searchButton = (Button) findViewById(R.id.search); searchButton.setOnClickListener(new ButtonClickListener()); helper = new DatabaseOpenHelper(NameToPinyinActivity.this); } ================================================================= public class DatabaseOpenHelper extends SQLiteOpenHelper { /** DB Name */ private static final String DB_NAME = "pinyin"; /** CREATE TABLE SQL */ private static final String CREATE_TABLE_SQL = "CREATE TABLE UNICODE_PINYIN" + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " + "UNICODE TEXT NOT NULL, PINYIN TEXT NOT NULL)"; public DatabaseOpenHelper(Context context) { super(context, DB_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.beginTransaction(); try { db.execSQL(CREATE_TABLE_SQL); db.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { db.endTransaction(); } } =================================================================解决方案
I have also had trouble with the SQLiteOpenHelper. What worked for me was storing a member variable
SQLiteDatabase db;In the SQLiteOpenHelper subclass and calling
db = getWritableDatabase();in the constructor.
The answer to this question also includes helpful information: SQLiteOpenHelper failing to call onCreate?
I hope this helps!
更多推荐
安卓SQLiteOpenHelper:的onCreate()方法没有被调用。为什么?
发布评论