简体   繁体   English

在Android的SQLite中创建数据库时出现问题

[英]Problem in creating a database in SQLite in Android

Hi I am new to android and I have a problem in creating a database. 嗨,我是Android新手,在创建数据库时遇到问题。

public class database extends ListActivity {
    /** Called when the activity is first created. */
    private final String MY_DATABASE_NAME = "myCoolUserDB.db";
    private final String MY_DATABASE_TABLE = "t_Users"; 
    Context c;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> results = new ArrayList<String>();
        setContentView(R.layout.main);
        SQLiteDatabase mydb=null;
        try
        {
            mydb.openOrCreateDatabase(MY_DATABASE_NAME,  null);

        } catch(Exception e){}
    }
}

When I run this code it throws a run time exception. 当我运行此代码时,它将引发运行时异常。 Please help me. 请帮我。

  1. If you are going to call a static method like openOrCreateDatabase, do it on the class (SQLiteDatabase.openOrCreateDatabase(...)), not an instance. 如果要调用像openOrCreateDatabase这样的静态方法,请在类(SQLiteDatabase.openOrCreateDatabase(...))上而不是在实例上进行操作。 It's a lot clearer - the way you've done it looks like you're calling an instance method, so looks like a sure NullPointerException, which of course is misleading. 这要清晰得多-完成它的方式看起来像是在调用实例方法,因此看起来像是肯定的NullPointerException,这当然会引起误解。

  2. As someone else has stated, the stack trace would be the most useful thing when asking for help with an exception. 正如其他人所说的那样,在寻求异常帮助时,堆栈跟踪将是最有用的东西。

  3. (Almost) never catch an exception without at the very least logging it. (几乎)至少在没有记录异常的情况下,绝不会捕获异常。 Don't just do nothing with it. 不要只是不做任何事情。 There are of course exceptions to every rule, but let's not go there for the moment. 当然,每条规则都有例外,但我们暂时不要去那里。 Anyway, if you don't at least log it, you're just throwing away information that would tell you what went wrong when everything goes to crap later. 无论如何,如果您至少没有将其记录下来,那么您只是在丢掉一些信息,这些信息会告诉您以后一切都出错时出了什么问题。

  4. You shouldn't be using that method directly, and should instead be extending SQLiteOpenHelper . 您不应该直接使用该方法,而应该扩展SQLiteOpenHelper See the android developers page on data storage to get started (I'd post a link but apparently I'm only allowed one link in my post ?!), and since you've probably had to download the SDK to get going, look in the samples that come with it for the Notepad sample application. 请参阅数据存储上的android开发人员页面以开始使用(我发布了一个链接,但显然我的帖子只允许一个链接?!),由于您可能必须下载SDK才能开始使用,在记事本示例应用程序随附的示例中。 That contains a NotePadProvider class, which is a good example of both a content provider and database access, which often go hand-in-hand on android. 其中包含一个NotePadProvider类,这是内容提供程序和数据库访问的一个很好的例子,它们通常在android上并存。 I'd suggest compiling that application and making some simple changes to it before you jump into making your own one. 我建议您编译该应用程序并对其进行一些简单的更改,然后再跳入自己的应用程序。

For working with sqlite database you need to create class extended from SQLiteOpenHelper: 为了使用sqlite数据库,您需要创建从SQLiteOpenHelper扩展的类:

private class DBHelper extends SQLiteOpenHelper {   

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(UPGRADE_TABLES);
    }

}   

Then you can get access to db using DbHelper object: 然后,您可以使用DbHelper对象访问数据库:

DBHelper dbHelper = new DBHelper(Activity.this);
SQLiteDatabase db = dbHelper.getReadableDatabase();

I run into the same problem. 我遇到了同样的问题。 It figures out that two bugs happens during development 它指出在开发过程中发生了两个错误

  • dir "databases" was not existent 目录“数据库”不存在
  • accendently ".db" was created as directory. 相应地,“。db”已创建为目录。

They following code cover both 他们遵循的代码涵盖了

File dbFile = getDatabasePath ("abc.db");

if (dbFile.isDirectory ()) {
    dbFile.delete();
}
if (! dbFile.exists()) {
    String path = dbFile.getParent ();
    new File (path).mkdirs ();
}
database  = SQLiteDatabase.openDatabase (dbFile.getAbsolutePath (), this, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);

Hope this helps 希望这可以帮助

I think SQLiteOpenHelper is only useful for "single table" databases. 我认为SQLiteOpenHelper仅对“单表”数据库有用。 For multiple table applications I consider directly using SQLiteDatabase fit better to a good architecture. 对于多表应用程序,我认为直接使用SQLiteDatabase更适合于良好的体系结构。

这是一篇简单的文章,告诉您如何在Android的SQLite数据库中插入数据 ,此外,此链接还向您展示了如何从Android的SQLite数据库检索数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM