使用SQLite android系统中包含它的单引号和双引号插入字符串 [英] insert string using sqlite in android containing single and double quotes in it

查看:225
本文介绍了使用SQLite android系统中包含它的单引号和双引号插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在插入一个Android应用程序使用SQLite字符串的麻烦,结果
我试过了,

 查询=插入或更换成表(_id,文本)VALUES(+ data.get(I).ID +,'+ data.get(I)的.text + ');
MyClass.db.execSQL(查询);

如果我的字符串的样子,

 我是一个Android开发者

应用程序崩溃这里,这里是logcat的结果,

 产生的原因:android.database.sqlite.SQLiteException:近M:语法错误:,在编译:插入或更换成表(_id,文本)VALUES(4' 我是一个Android开发者)

我觉得它假设,我的查询到此结束

 

请帮我插入字符串的任何情况下,无论是它包含了像单或双引号,

 我是一个机器人开发者


解决方案

在没有任何硬编码或任何你可以直接使用 ContentValues​​ 象下面插入。

  ContentValues​​值=新ContentValues​​();
长retvalue = 0;
values​​.put(_ ID,id_here);
values​​.put(文字,your_text_here);
retvalue = MyClass.db.insertWithOnConflict(表,空,价值观,CONFLICT_REPLACE);

I am having trouble with inserting a string using sqlite in an android app,
I tried,

query  = "INSERT OR REPLACE into table(_id, text) VALUES ("+data.get(i).id+", '"+data.get(i).text+"')";
MyClass.db.execSQL(query);

If my string looks like,

'I'm an android developer'

App crashes here, here is logcat result,

Caused by: android.database.sqlite.SQLiteException: near "m": syntax error: , while compiling: INSERT OR REPLACE into table (_id, text) VALUES (4, '"I'm an android developer"' )

I think it assumes that, my query ends here

'"I'

please help me to insert any case of string, either it contains single or double quotes like,

"I'm an "android" developer"

解决方案

Without any hardcoding or anything you can directly insert with using ContentValues like below..

ContentValues values = new ContentValues();
long retvalue = 0;
values.put("_id", id_here);
values.put("text", your_text_here);
retvalue = MyClass.db.insertWithOnConflict(table, null, values, CONFLICT_REPLACE);

这篇关于使用SQLite android系统中包含它的单引号和双引号插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆