问题在Android的SQLite中创建数据库 [英] Problem in creating a database in SQLite in Android

查看:144
本文介绍了问题在Android的SQLite中创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的Andr​​oid和我在创建数据库的一个问题。

 公共类数据库扩展ListActivity {
    / **当第一次创建活动调用。 * /
    私人最终字符串MY_DATABASE_NAME =myCoolUserDB.db;
    私人最终字符串MY_DATABASE_TABLE =t_Users;
    上下文℃;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        ArrayList的<串GT;结果=新的ArrayList<串GT;();
        的setContentView(R.layout.main);
        SQLiteDatabase MYDB = NULL;
        尝试
        {
            mydb.openOrCreateDatabase(MY_DATABASE_NAME,NULL);        }赶上(例外五){}
    }
}

当我运行这个code,它抛出一个运行时异常。请帮我。


解决方案

  1. 如果你要调用像openOrCreateDatabase一个静态方法,这样做的类(SQLiteDatabase.openOrCreateDatabase(...)),不是一个实例。这是一个更加清晰 - 你做它看起来像你调用一个实例方法的方式,所以看起来像一个确定NullPointerException异常,这当然是一种误导


  2. 由于别人已经指出的那样,堆栈跟踪将与例外不禁要问时最有用的东西。


  3. (几乎)从来没有捕获异常没有最起码记录日志。不要只是做与它无关。当然也有例外,每一个规则的,但我们不要去那里的时刻。无论如何,如果你没有,至少记录它,你只是扔掉信息会告诉你什么地方错了,当一切都以后废话。


  4. 您不应该直接被使用该方法,而应被延长的 SQLiteOpenHelper 。查看数据存储在Android开发者页面上手(我会发布一个链接,但显然我只允许在我的后一个环节?!),既然你可能不得不下载SDK得走了,看随之而来的记事本示例应用程序样本英寸包含NotePadProvider类,这既是一个内容提供商和数据库访问的一个很好的例子,它经常去手牵手在Android上。我建议在编译应用程序,并做一些简单的修改它,你跳进让你自己一个人了。


Hi I am new to android and I have a problem in creating a database.

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. 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.

  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 . 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. 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. I'd suggest compiling that application and making some simple changes to it before you jump into making your own one.

这篇关于问题在Android的SQLite中创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆