5

大家好,我对在 android 中进行开发非常陌生,我想在我的应用程序中添加一个数据库。

问题是我不知道如何只初始化整个表一次。

我做了很多阅读,我发现你可以onCreate(SQLiteDatabase db)在辅助类中的方法的覆盖中做到这一点。

这些是我的数据字段和我的onCreate(SQLiteDatabase db)方法。

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_EIGHTU = "8u";
public static final String KEY_NINEU = "9u";
public static final String KEY_TENU = "10u";
public static final String KEY_ELEVENU = "11u";
private static final String TAG = "DBAdapter"; 

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

但不知道如何..有什么想法吗?

主要问题是我有多个要初始化的行。

4

5 回答 5

6

其他答案似乎没有回答如何用初始值实际填充数据库,所以我将在这里分享我是如何做到的。

基本上,您将数据库列保存为 xml 文件中的字符串数组。我将只显示一个数组,但您可以为其他列做更多。(不过,您需要小心,不要使用多列来弄乱数组顺序,从而弄乱您的数据库行。)

创建一个 /res/values/arrays.xml 文件并将数组添加到其中。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="my_array">

            <item>string 1</item>
            <item>string 2</item>
            <item>string 3</item>
            <item>string 4</item>
            <item>string 5</item>

    </string-array>

</resources>

然后,您可以在帮助程序类中创建数据库时获取该数组。这是我所做的:

private static class DatabaseHelper extends SQLiteOpenHelper {

    private final Context fContext;

    // constructor
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        fContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                + FIRST_COLUMN + " INTEGER PRIMARY KEY,"
                + SECOND_COLUMN + " TEXT NOT NULL,"
                + THIRD_COLUMN + " INTEGER,"
                + FOURTH_COLUMN + " TEXT NOT NULL"
                + ");");

        // Initialize the db with values from my_array.xml
        final int DEFAULT_THIRD_COLUMN = 1;
        final String DEFAULT_FOURTH_COLUMN = "";
        ContentValues values = new ContentValues();
        Resources res = fContext.getResources();
        String[] myArray = res.getStringArray(R.array.my_array);
        for (String item : myArray){
            values.put(SECOND_COLUMN, item);
            values.put(THIRD_COLUMN, DEFAULT_THIRD_COLUMN);
            values.put(FOURTH_COLUMN, DEFAULT_FOURTH_COLUMN);
            db.insert(TABLE_NAME, null, values);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

    }
}

有关更多帮助,请参阅这些链接:

我做的另一件事是提前创建数据库,然后在安装应用程序时将其复制到数据库文件夹。如果您有大量数据并且不想从 Internet 下载数据库,这可能会更好。如果要复制数据库,请参阅此答案

于 2014-09-09T04:41:06.293 回答
4

试试这种方式:

public class MyDBHelper  
{  
    // Database properties   
    private static final String DATABASE_NAME = "Test.sqlite";   
    private static final String DATABASE_TABLE_NAME = "Table 1";   
    private static final int DATABASE_VERSION = 4;

    //  Table1 properties   
    public static final String KEY_ROWID = "_id";   
    public static final String KEY_NAME = "name";   
    public static final String KEY_EIGHTU = "8u";   
    public static final String KEY_NINEU = "9u";      
    public static final String KEY_TENU = "10u";   
    public static final String KEY_ELEVENU = "11u";  
    private static final String TAG = "DBAdapter";

    // Create Script   
    private static final String DATABASE_CREATE_PLAYER = "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE_NAME + "( " + KEY_ROWID + " INTEGER, " + KEY_NAME + " TEXT, " + KEY_EIGHTU + " TEXT, " + KEY_NINEU + " TEXT, " + KEY_TENU + " TEXT, " + KEY_ELEVENU + " TEXT);";        
    private final Context m_context;  
    private DatabaseHelper m_dbHelper;   
    public static SQLiteDatabase m_db;   
    public MyDBHelper(Context ctx)  
    {   
        this.m_context = ctx;  
        m_dbHelper = new DatabaseHelper(m_context);  
    }   

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

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

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NAME );
            onCreate(db);
        }
    }

    //---opens the database---
    public MyDBHelper open() throws SQLException
    {
        m_db = m_dbHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close()
    {
        if (m_db != null)
            m_db.close();
        if (m_dbHelper != null)
            m_dbHelper.close();
    }  
}
于 2013-01-01T06:03:24.123 回答
0

如果您想在代码中填充它,本教程将有所帮助。这是一个包含 14 个视频教程的系列。

http://thenewboston.org/watch.php?cat=6&number=111

如果您想访问预加载的数据库,请查看此链接。

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

但两者都没有解决平板电脑所需的内容提供商的使用问题。但是两者都可以帮助您入门,一旦您了解了它们,您就可以了解提供者。

于 2013-01-01T05:56:27.540 回答
0

onCreate()在方法之前和之后写下private static final String TAG = "DBAdapter";一行,它将解决您的问题。

private static final String DATABASE_CREATE = "create table mTable1 
                   (_id integer primary key autoincrement, 
                   name text not null, 8u text not null, 9u text not null, 
                   10u text not null, 11u text not null);";
于 2013-01-01T06:03:21.170 回答
0
import java.util.ArrayList;
import java.util.List;

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

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

您也可以访问这些网站以获得完整的参考和更好的理解::

androidhive.info/Sqlite 教程-

于 2013-01-01T06:06:21.437 回答