数据库不是在android中创建的 [英] database is not created in android

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

问题描述

在我的应用程序活动中运行良好,但没有创建数据库,logcat 中也没有错误.主要活动类:

In my application activity was running fine but database is not getting created also there is no error in logcat. Main activity class:

package com.example.testdb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Database d=new Database(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

数据库类:

package com.example.testdb;

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

public class Database extends SQLiteOpenHelper{

    String Tablename = "Table1";
    private String Column1 = "RegionID";
    private String Column2 = "RegionName";
    private String Column3 = "Currency";
    SQLiteDatabase db;

    public Database(Context context) {
        super(context, "Test", null, 2);
        this.getWritableDatabase();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
            final String r_Table = "CREATE TABLE " + Tablename + " (" + Column1+ " INTEGER PRIMARY KEY , " + Column2 + " TEXT, " + Column3 + " Text) ";
            db.execSQL(r_Table);
            ContentValues cv = new ContentValues();
            cv.put(Column1, 1);
            cv.put(Column2, "India");
            cv.put(Column3, "Rupee");
            db.insert(r_Table, null, cv);
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }

        Cursor c = db.rawQuery("Select * from Table1", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                //Toast.makeText(new MainActivity().getApplicationContext(), c.getCount(), Toast.LENGTH_LONG).show();
                System.out.println("Rows are:"+c.getCount());
            }
        }
        c.close();  
    }

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

Logcat:

05-18 12:45:22.042: I/ActivityThread(3270): Switching default density from 160 to 130
05-18 12:47:18.162: I/ActivityThread(3415): Switching default density from 160 to 130

我在模拟器中使用 Virtual box,在 DDMS 中我什至看不到创建的数据库.

I am using Virtual box for emulator and in DDMS I can't even see the database got created.

谁能帮我解决这个问题.

Can anyone please help me in this.

提前致谢湿婆

推荐答案

根据文档 SqliteOpenHelper 使用惰性模型创建数据库.这意味着,在没有数据库的情况下,不会在构造函数中调用 onCreate,而是在实际需要数据库时调用,即在第一次调用 getReadableDatabasegetWritableDatabase 时.所以,你的情况是正常的.

According to documentation SqliteOpenHelper uses lazy model of creating databases. This means, that in the absence of database, onCreate will be called not in constructor, but when the database is actually needed, I.e. at the first call to getReadableDatabase or getWritableDatabase. So, you situation is normal.

将 SqliteOpenHelper 视为数据库连接的提供者,包含用于处理如果数据库不存在"和如果数据库已过时"等情况的附加逻辑.因此,在您的情况下,不要担心实际创建数据库文件的时间.

Think of SqliteOpenHelper as provider of connection to database, containing additional logic for handling situations like "if database not exist" and "if database is outdated". Thus, in your case, don't worry about when database file is actually created.

您的 Activity 中的数据库使用情况(如果它是唯一的,则使用 db)可能类似于:

Database usage in your Activity (if it is the only one, using db) could be something like:

private SqliteOpenHelper connection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.OnCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    connection = new Database(this); //no actual db creation here
    //if you want to test db table creation without any further work uncomment following line
    //SQLiteDatabase db = connection.getReadableDatabase();
}

public void onBtnAddRecordClick(View view) {
    SqliteDatabase db = connection.getWritableDatabase(); //if something not right with
                                                  //the db this will be corrected here
    ContentValues values;
    //... fill in the record to insert
    db.insert(MY_TABLE_NAME, null, values);
}

重要说明:如果您打算从多个活动访问您的数据库,请考虑使您的数据库扩展 SqliteDatabaseHelper 成为单例或将其包装到一个 ContentProvider 中,因为通常不赞成使用多个连接到一个没有明确目的的数据库.

Important note: If you are going to access you db from multiple Activities, consider making your Database extending SqliteDatabaseHelper a singleton or wrap it into a ContentProvider, because it is generally frowned upon to use multiple connections to a database without a clear purpose.

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

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