通过 xml 资源中包含的数组初始化 sqlite table
Initialize an sqlite table through an array contained in a xml resource
我必须在 android 中初始化一个 sqllite 数据库。
这是 table:
的结构
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
}
我有一个 array.xml 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<string-array name="my_array">
<item>S1 999</item>
<item>S1 10</item>
<item>S1 111</item>
<item>S1 101</item>
</string-array>
现在我想获取此资源中的项目,将它们分成两部分并使用结果来初始化我的数据库。我有一个函数可以解决问题:
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
ContentValues values = new ContentValues();
Resources res = fContext.getResources();
String[] myArray = res.getStringArray(R.array.my_array);
for (String item : myArray){
System.out.println(item);
String[] split = item.split(" ");
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, split[1]);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, split[0]);
db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
}
问题是没有得到预期的结果
S1 999
S1 10
S1 111
S1 101
我得到以下信息:
1 1
2 1
3 1
4 1
实际上,System.out.println(item);
打印的每一项都是由一对二值组成的,如1 1, 2 1
等。我应该如何修改 java 方法 onCreate 以获得正确的字符串对,例如 S1 999
?
下面是我如何读取 table 中的数据:
String[] projection = {
FeedReaderContract.FeedEntry._ID,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
};
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder
);
String result = "";
DatabaseUtils.dumpCursor(cursor);
int iRow = cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID);
int iName = cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + "\n";
}
System.out.println(result);
: 我有一个游标,它向数据库发送请求,然后打印游标的内容。
您的字符串拆分不正确。要按 space 拆分字符串,请使用以下代码。
str = "This is the string seperated by space";
String[] splited = str.split("\s+");
我必须在 android 中初始化一个 sqllite 数据库。 这是 table:
的结构 public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
}
我有一个 array.xml 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<string-array name="my_array">
<item>S1 999</item>
<item>S1 10</item>
<item>S1 111</item>
<item>S1 101</item>
</string-array>
现在我想获取此资源中的项目,将它们分成两部分并使用结果来初始化我的数据库。我有一个函数可以解决问题:
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
ContentValues values = new ContentValues();
Resources res = fContext.getResources();
String[] myArray = res.getStringArray(R.array.my_array);
for (String item : myArray){
System.out.println(item);
String[] split = item.split(" ");
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, split[1]);
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, split[0]);
db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
}
问题是没有得到预期的结果
S1 999
S1 10
S1 111
S1 101
我得到以下信息:
1 1
2 1
3 1
4 1
实际上,System.out.println(item);
打印的每一项都是由一对二值组成的,如1 1, 2 1
等。我应该如何修改 java 方法 onCreate 以获得正确的字符串对,例如 S1 999
?
下面是我如何读取 table 中的数据:
String[] projection = {
FeedReaderContract.FeedEntry._ID,
FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
};
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder
);
String result = "";
DatabaseUtils.dumpCursor(cursor);
int iRow = cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID);
int iName = cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + "\n";
}
System.out.println(result);
: 我有一个游标,它向数据库发送请求,然后打印游标的内容。
您的字符串拆分不正确。要按 space 拆分字符串,请使用以下代码。
str = "This is the string seperated by space";
String[] splited = str.split("\s+");