我有一个数组。并且数组的每个单元格都有一些文本,并且可以包含换行符(\ n)。我需要从这个数组插入数据到sqlite数据库。我已经做了。但是当我使用来自db的数据时我会丢失换行符号,并且我在一行上获得数据。如何解决这个问题。 将数据插入数据库:
public void insert(String key, String value) {
String sql = "INSERT INTO words(key, value) VALUES(?,?)";
Connection conn = this.connect();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,key);
pstmt.setString(2,value);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws IOException, InterruptedException{
Main app = new Main();
while(values.size() > 0) {
System.out.println("db: " + ++c);
app.insert(words.poll(), values.poll());
}
}
在我的Android应用程序上从db获取数据:
final Word word = mDBHelper.getWord(value, mDicType);
tvWordTranslate = (WebView)view.findViewById(R.id.tvWordTranslate);
String text = "<html><head>"
+ "<style type=\"text/css\">body{color: #27009b; background-color: #FFFFFF;}"
+ "</style></head>"
+ "<body>"
+ word.value+ "</body></html>";
tvWordTranslate.loadDataWithBaseURL(null, text, "text/html", "utf-8", null);
// word.value可以包含换行符 从db获取单词的函数:
public Word getWord(String key, int dicType) {
String tableName = getTableName(dicType);
String q = "SELECT * FROM " + tableName + " WHERE upper([key]) = upper(?)";
Cursor result = mDB.rawQuery(q, new String[]{key});
Word word = new Word();
while(result.moveToNext()) {
word.key = result.getString(result.getColumnIndex(COL_KEY));
word.value = result.getString(result.getColumnIndex(COL_VALUE));
}
return word;
}