无法做第二个表中的onCreate()的Andr​​oid [英] unable to make second table in onCreate() android

查看:140
本文介绍了无法做第二个表中的onCreate()的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进口android.content.ContentValues​​;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;    公共类Databasehelper扩展SQLiteOpenHelper {    公共静态最后弦乐DNAME =info.db;
    公共静态最后弦乐TABLE_NAME =EXP;
    公共静态最后弦乐COL1 =类别;
    公共静态最后弦乐COL2 =STATUS;
    公共静态最后弦乐COL3 =AMT
    公共静态最后弦乐COL4 =DES;    公共静态最后弦乐table2_name =过去;
    公共静态最后弦乐COL12 =past_exp;
    公共静态最后弦乐CREATE_FIRST_TABLE =
            CREATE TABLE+ TABLE_NAME +(+
                    COL1 +VARCHAR(25),+
                    COL2 +VARCHAR(10),+
                    COL3 +浮动(5,3),+
                    COL4 +VARCHAR(255));;    公共静态最后弦乐CREATE_SECOND_TABLE =
            CREATE TABLE+ table2_name +(+
                    COL12 +浮动(10,2)NOT NULL DEFAULT'0.0');;    公共Databasehelper(上下文的背景下){
        超级(上下文,DNAME,空,1);    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        // TODO自动生成方法存根
        db.execSQL(CREATE_FIRST_TABLE);
        db.execSQL(CREATE_SECOND_TABLE);
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        // TODO自动生成方法存根    db.execSQL(DROP TABLE IF EXISTS+ table_name的);
    的onCreate(DB);    db.execSQL(DROP TABLE IF EXISTS+ table2_name);
    的onCreate(DB);    }    公共布尔insertData(字符串类型,字符串状态,双AMT,
            字符串DES){
        SQLiteDatabase分贝= this.getWritableDatabase();
        ContentValues​​ CV =新ContentValues​​();
        cv.put(COL1,类别);
        cv.put(COL2,地位);
        cv.put(COL3,AMT);
        cv.put(COL4,DES);        长期结果= db.insert(TABLE_NAME,空,CV);        如果(结果==( - 1)){
            返回false;
        }其他{
            返回true;
        }
    }    公共布尔insertData2(浮点EXP){
        SQLiteDatabase分贝= this.getWritableDatabase();
        ContentValues​​ CV =新ContentValues​​();
        cv.put(COL12,EXP);        长期结果= db.insert(table2_name,空,CV);        如果(结果==( - 1)){
            返回false;
        }其他{
            返回true;
        }
    }    公共光标的getData(){
        SQLiteDatabase分贝= this.getWritableDatabase();
        光标C = db.rawQuery(SELECT * FROM+ table_name的,NULL);
        返回℃;
    }    公共光标getData2(){
        SQLiteDatabase分贝= this.getWritableDatabase();
        光标C = db.rawQuery(SELECT * FROM+ table2_name,NULL);
        返回℃;
    }
}

我需要两个表详和的过去。我已经使用了的onCreate(Sqlitdatabase DB)的exec方法创建两个表,但没有得到创建的第二个表和应用程序崩溃。当在的onCreate 叫什么呢?我已经创造了2不同类别2 databasehelpers和使用它们一个接一个。这是我的数据库辅助类...我如何使用相同的的onCreate 方法在数据库辅助类2表中采用上述

给出

我们需要创建2个表或1就足够了2数据库的帮手?
请尽快回复越好。我如何使用相同的的onCreate 方法在数据库中的辅助类2表?

logcat的错误:

  20 09-12:33:19.178:E / AndroidRuntime(21084):android.database.sqlite.SQLiteException:没有这样的表:过去(code 1产生的原因),在编译:从过去的*
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteConnection.native prepareStatement(本机方法)
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteConnection.acquire preparedStatement(SQLiteConnection.java:882)
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteConnection prepare(SQLiteConnection.java:493)
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteSession prepare(SQLiteSession.java:588)
。09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteProgram<&初始化GT;(SQLiteProgram.java:58)
。09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteQuery<&初始化GT;(SQLiteQuery.java:37)
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
09-12 20:33:19.178:E / AndroidRuntime(21084):在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)


解决方案

当是确切的onCreate叫?

本的onCreate(SQLiteDatabase DB)方法在第一时间应用程序与数据库交互执行。 <一href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onCreate(android.database.sqlite.SQLiteDatabase)\"相对=nofollow> DOC。

我们需要2数据库助手创建2个表或1就足够了?

使用1 DatabaseHelper你可以创造你想要的表。你甚至可以创建一个数据库倍数具有一个DatabaseHelper。

我如何使用相同的onCreate 2表?

有与您的onCreate()方法没有任何问题。

关于logcat的错误:

该给什么方法,你的logcat的错误?你肯定当你调用这个方法您的数据库中创建?

我做一个测试应用程序,您的code运行,因为它应该。也许你只在第一次运行的您的应用程序的第一个表创建数据库,并在此之后,您编辑您的code和添加第二个表,第二个选择,但第二次运行你的应用程序数据库将不会被创建,也没有更新。如果我是正确的,如果你改变构造函数和数据库版本您创建querys痘痘改变你的应用程序应该运行。

您可以阅读文档 rel=\"nofollow\">。

 公共静态最后弦乐CREATE_FIRST_TABLE =
            CREATE TABLE IF NOT EXISTS+ table_name的+(+
                    COL1 +VARCHAR(25),+
                    COL2 +VARCHAR(10),+
                    COL3 +浮动(5,3),+
                    COL4 +VARCHAR(255));;    公共静态最后弦乐CREATE_SECOND_TABLE =
            CREATE TABLE IF NOT EXISTS+ table2_name +(+
                    COL12 +浮动(10,2)NOT NULL DEFAULT'0.0');;    公共Databasehelper(上下文的背景下){
        //最后一个数字是database_version。
        //如果您的应用程序已经instaled和DB创建
        //你需要更改或创建新表,你只是增加了最后一个号码。
        //(请​​注意,您不能降级在这个方法的数据库版本。
        //如果由于某种原因,你需要dowgrade分贝,你需要重写onDowngrade())
        超级(上下文,DNAME,空,1);
    }

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

    public class Databasehelper extends SQLiteOpenHelper {

    public static final String Dname="info.db";
    public static final String table_name="exp";
    public static final String col1="CATEGORY";
    public static final String col2="STATUS";
    public static final String col3="AMT";
    public static final String col4="DES";

    public static final String table2_name="past";
    public static final String col12="past_exp";


    public static final String CREATE_FIRST_TABLE =
            "CREATE TABLE " + table_name + " (" +
                    col1 + " varchar(25), " +
                    col2 + " varchar(10), " +
                    col3 + " float(5,3), " +
                    col4 + " varchar(255));";

    public static final String CREATE_SECOND_TABLE =
            "CREATE TABLE " + table2_name + " (" + 
                    col12 + " float(10,2) NOT NULL DEFAULT '0.0');";

    public Databasehelper(Context context) {
        super(context, Dname, null, 1);

    }

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

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

    db.execSQL("DROP TABLE IF EXISTS "+table_name);
    onCreate(db);

    db.execSQL("DROP TABLE IF EXISTS "+table2_name);
    onCreate(db);

    }

    public boolean insertData(String category,String status,double amt,
            String des) {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(col1, category);
        cv.put(col2, status);
        cv.put(col3, amt);
        cv.put(col4, des);

        long result=db.insert(table_name, null, cv);

        if (result==(-1)) {
            return false;
        } else {
            return true;
        }
    }

    public boolean insertData2(float exp) {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(col12, exp);

        long result=db.insert(table2_name, null, cv);

        if (result==(-1)) {
            return false;
        } else {
            return true;
        }
    }

    public Cursor getData() {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor c=db.rawQuery("select * from "+table_name, null);
        return c;           
    }

    public Cursor getData2() {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor c=db.rawQuery("select * from "+table2_name, null);
        return c;           
    }
}

I need to make two tables-exp and past. I have created the two tables using exec in the onCreate(Sqlitdatabase db) method, but the second table is not getting created and the app crashes. When is the onCreate called exactly? I have created 2 databasehelpers in 2 different classes and using them one after the other. This is my database helper class... How do i make 2 tables using the same onCreate method in the database helper class that is given above

Do we need 2 database helpers for creating 2 tables or 1 is sufficient? Please reply as soon as possible. How do i make 2 tables using the same onCreate method in the database helper class?

logcat error:

09-12 20:33:19.178: E/AndroidRuntime(21084): Caused by: android.database.sqlite.SQLiteException: no such table: past (code 1): , while compiling: select * from past
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
09-12 20:33:19.178: E/AndroidRuntime(21084):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)

解决方案

"When is the onCreate called exactly?"

The onCreate(SQLiteDatabase db) method is executed in the first time your application interact with the database. DOC.

"Do we need 2 database helpers for creating 2 tables or 1 is sufficient?"

With 1 DatabaseHelper you can create as many tables you want. You can even create a multiples databases with one DatabaseHelper.

"How do i make 2 tables using the same onCreate?"

There is no problem with your onCreate() method.

About the logcat error:

What method the give you the logcat error? Are you sure that your db is created when you call that method?

I make one test app and your code run as it should. Maybe you created the database with only the first table in the "first time run" of your app, and after that, you edited your code and added the second table and the second select, but the second time you run your app the database will not be created, nor updated. If I'm right your app should run if you change the database version on constructor and a litle change on your create querys.

You can read the documentation here.

public static final String CREATE_FIRST_TABLE =
            "CREATE TABLE IF NOT EXISTS " + table_name + " (" +
                    col1 + " varchar(25), " +
                    col2 + " varchar(10), " +
                    col3 + " float(5,3), " +
                    col4 + " varchar(255));";

    public static final String CREATE_SECOND_TABLE =
            "CREATE TABLE IF NOT EXISTS " + table2_name + " (" +
                    col12 + " float(10,2) NOT NULL DEFAULT '0.0');";

    public Databasehelper(Context context) {
        // The last number is the database_version. 
        // If your app was already instaled and the db created 
        // and you need to change or create new tables, you just increase the last number. 
        // (Be aware that you can't downgrade the database version in this method. 
        // If for some reason you need to dowgrade the db, you will need to override the onDowngrade())
        super(context, Dname, null, 1); 
    }

这篇关于无法做第二个表中的onCreate()的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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