安卓SQLiteOpenHelper:的onCreate()方法没有被调用。为什么?

编程入门 行业动态 更新时间:2024-10-25 16:24:19
本文介绍了安卓SQLiteOpenHelper:的onCreate()方法没有被调用。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让我的第一个Android应用程序。我听说,如果没有创建数据库的SQLiteOpenHelper.onCreate()方法的过程创建表。然而,以为我试图调试的onCreate()方法没有奏效,甚至。请看看下面的code和给我任何建议。任何帮助将AP preciated。

====================================== =========================== 公共类NameToPinyinActivity延伸活动{     DatabaseOpenHelper帮手= NULL;     @覆盖     公共无效的onCreate(包savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.nametopinyin);         按钮searchButton =(按钮)findViewById(R.id.search);         sea​​rchButton.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()方法没有被调用。为什么?

本文发布于:2023-11-03 15:57:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   SQLiteOpenHelper   onCreate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!