大家好我很想开发android,我想在我的应用程序中添加一个数据库。
问题是我不知道如何只对整个表进行一次初始化。
我做了很多阅读,我发现你可以做到
覆盖helper类中的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);
}
但我不确定如何......任何想法?
主要问题是我想要初始化多行。
答案 0 :(得分: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();
}
}
答案 1 :(得分:4)
其他答案似乎没有回答如何用初始值实际填充数据库,所以我将在此分享我是如何做到的。
基本上,您将数据库列保存为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下载数据库,那么这可能会更好。如果要复制数据库,请参阅this answer。
答案 2 :(得分:0)
如果你想在代码中填充它,本教程将有所帮助。这是一系列14个视频教程。
http://thenewboston.org/watch.php?cat=6&number=111
如果要访问预加载的数据库,请查看此链接。
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
但是,两者都没有涉及使用平板电脑所需的内容提供商。但两者都会让你开始,一旦你学会了它们,你就可以学习提供者。
答案 3 :(得分: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);";
答案 4 :(得分: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);
}
您也可以访问这些网站以获得完整的参考和更好的理解::