android sqlite3 中文乱码问题,恳求~
我在android中,用sqlite3存储了中文,但是数据库里展示是乱码,在android里取出来也是乱码,求解救~建表语句:sql="CreatetableIFNOTE...
我在android中,用sqlite3存储了中文,但是数据库里展示是乱码,在android里取出来也是乱码,求解救~
建表语句:
sql="Create table IF NOT EXISTS question_tb(" +
"code int," +
"is_finished int," +
"is_english int,"+
"pic_name varchar(64)," +
"ans_back BLOB," +
"ans_true varchar(64)," +
"info int"+
")";
ans_back 和pic_name是需要存储中文的,两种自动类型都试过,不行
插入是这样插入的
db.execSQL("insert into question_tb values(?,?,?,?,?,?,?)",value);
读取是这样读的
android.util.Log.d("tag", String.valueOf(i)+":"+cursor.getString(cursor.getColumnIndex("ans_back")));
也试过这种
byte[] val = cursor.getBlob(cursor.getColumnIndex("ans_back"));
String sn;
try {
sn = new String(val,"gb2312");
android.util.Log.d("tag",sn);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是结果都是乱码,求解救啊
貌似还是不行哦,例如中文是:“小明小黄小李小陈”
插入做了转换
for(int i=0;i<value.length;i++)
insertvalue[i]=new String(value[i].getBytes(),"UTF-8");
db.execSQL("insert into question_tb values(?,?,?,?,?,?,?)",insertvalue);
读取的时候,转换和不转换展示的都是乱码
1.这样是“С��С��С��С��”
android.util.Log.d("tag",cursor.getString(cursor.getColumnIndex("ans_back")));
2.这样是:С��С��С��С����
try {sn = new String(val,"UTF-8");
android.util.Log.d("tag",sn);} 展开
建表语句:
sql="Create table IF NOT EXISTS question_tb(" +
"code int," +
"is_finished int," +
"is_english int,"+
"pic_name varchar(64)," +
"ans_back BLOB," +
"ans_true varchar(64)," +
"info int"+
")";
ans_back 和pic_name是需要存储中文的,两种自动类型都试过,不行
插入是这样插入的
db.execSQL("insert into question_tb values(?,?,?,?,?,?,?)",value);
读取是这样读的
android.util.Log.d("tag", String.valueOf(i)+":"+cursor.getString(cursor.getColumnIndex("ans_back")));
也试过这种
byte[] val = cursor.getBlob(cursor.getColumnIndex("ans_back"));
String sn;
try {
sn = new String(val,"gb2312");
android.util.Log.d("tag",sn);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是结果都是乱码,求解救啊
貌似还是不行哦,例如中文是:“小明小黄小李小陈”
插入做了转换
for(int i=0;i<value.length;i++)
insertvalue[i]=new String(value[i].getBytes(),"UTF-8");
db.execSQL("insert into question_tb values(?,?,?,?,?,?,?)",insertvalue);
读取的时候,转换和不转换展示的都是乱码
1.这样是“С��С��С��С��”
android.util.Log.d("tag",cursor.getString(cursor.getColumnIndex("ans_back")));
2.这样是:С��С��С��С����
try {sn = new String(val,"UTF-8");
android.util.Log.d("tag",sn);} 展开
2个回答
展开全部
楼主,插入字符的编码和读出时用的编码要一致,就是说:
假设,数据库中数据是你通过android代码插入,以GB2312编码格式,如:
db.execSQL("insert into question_tb values(?,?,?,?,?,?,?)",new String(val,"gb2312"));
插入到表:
"Create table IF NOT EXISTS question_tb(" +
"code int," +
"is_finished int," +
"is_english int,"+
"pic_name varchar(64)," +
"ans_back BLOB," +
"ans_true varchar(64)," +
"info int"+
")";
读出就是:
byte[] val = cursor.getBlob(cursor.getColumnIndex("ans_back"));
String sn;
try {
sn = new String(val,"gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
就是说,插入和读取要么都做转换,要么都不做(android默认UTF-8格式)。
更多追问追答
追问
那么,意思是,如果我插入时,用utr8插入,读出来就不用转换了?
追答
en
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询