Java 如何在HTML.toHTML()中使用字符串变量?

Java 如何在HTML.toHTML()中使用字符串变量?,java,android,android-sqlite,Java,Android,Android Sqlite,我的应用程序中有一个EditText,用于将其内容作为HTML存储到SQLite数据库 当我在文本中添加撇号(例如Ajinkya的应用程序)时,EditText数据不会被插入到数据库中,因为它将第一个撇号视为查询中断 我在这里尝试了许多不同的建议解决方案,但没有成功。例如,我将“\”替换为“\”,但代码(请参见下一个代码块)在转换为html后仍然没有替换该符号 String NotesContent=Html.toHtml(notes\u content.getText()); 字符串Notes

我的应用程序中有一个EditText,用于将其内容作为HTML存储到SQLite数据库

当我在文本中添加撇号(例如Ajinkya的应用程序)时,EditText数据不会被插入到数据库中,因为它将第一个撇号视为查询中断

我在这里尝试了许多不同的建议解决方案,但没有成功。例如,我将“\”替换为“\”,但代码(请参见下一个代码块)在转换为html后仍然没有替换该符号

String NotesContent=Html.toHtml(notes\u content.getText());
字符串Notestitle=Html.toHtml(notes_title.getText());
if(Notestitle.contains(“')){
Notestitle=Notestitle.replace(“'”,“\”);
}
if(NotesContent.contains(“')){
NotesContent=NotesContent.replace(“'”,“\”);
}
因此,当我将EditText数据保存到字符串变量中,然后将其转换为HTML时,我尝试先用“\”替换“'”(请参见下一个代码块)。但是函数HTML.toHTML()不接受字符串变量

String NotesContent=notes\u content.getText().toString();
字符串Notestitle=notes_title.getText().toString();
if(Notestitle.contains(“')){
Notestitle=Notestitle.replace(“'”,“\”);
}
if(NotesContent.contains(“')){
NotesContent=NotesContent.replace(“'”,“\”);
}
Notestitle=Html.toHtml(Notestitle);//不接受字符串!
请帮助我解决这个问题。

如果使用conveniece方法,这将转义字符串

例如(当我在文本中放置撇号时(例如Ajinkya的应用程序),EditText数据不会被插入到数据库中,因为它将第一个撇号视为查询中断(注意,添加了一些HTML)作为要存储和检索的数据):-

数据库助手DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "mydb";
    public static final int DBVERSION = 1;

    public static final String TABLE = "htmlstore";
    public static final String IDCOLUMN = BaseColumns._ID;
    public static final String HTMLCOLUMN = "html";

    SQLiteDatabase db;

    public DatabaseHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        db = this.getWritableDatabase();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String crt_htmlstore_table = "CREATE TABLE IF NOT EXISTS " + TABLE + "(" +
                IDCOLUMN + " INTEGER PRIMARY KEY," +
                HTMLCOLUMN + " TEXT" +
                ")";
        db.execSQL(crt_htmlstore_table);
    }


    public long insert(String html) {
        ContentValues cv = new ContentValues();
        cv.put(HTMLCOLUMN,html);
        return db.insert(TABLE,null,cv); //<<<< does the escaping for you
    }

    public String getHTML(long id) {
        String rv = "";
        Cursor csr = db.query(TABLE,new String[]{HTMLCOLUMN},IDCOLUMN+"=?",new String[]{String.valueOf(id)},null,null,null);
        if (csr.moveToFirst()) {
            rv = csr.getString(csr.getColumnIndex(HTMLCOLUMN));
        }
        csr.close();
        return rv;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
public类DatabaseHelper扩展了SQLiteOpenHelper{
公共静态最终字符串DBNAME=“mydb”;
公共静态最终int DBVERSION=1;
公共静态最终字符串表=“htmlstore”;
公共静态最终字符串IDCOLUMN=BaseColumns.\u ID;
公共静态最终字符串HTMLCOLUMN=“html”;
sqlitedb数据库;
公共数据库助手(上下文){
super(上下文,DBNAME,null,DBVERSION);
db=this.getWritableDatabase();
}
@凌驾
public void onCreate(SQLiteDatabase db){
字符串crt\U htmlstore\U table=“创建不存在的表格”+表格+”(“+
IDCOLUMN+“整数主键,”+
HTMLCOLUMN+“文本”+
")";
execSQL(crt\u htmlstore\u表);
}
公共长插入(字符串html){
ContentValues cv=新的ContentValues();
cv.put(HTMLCOLUMN,html);

返回db.insert(TABLE,null,cv);//将
'
替换为
'
(两次撇号)在db.Hi Fantomas中插入字符串之前,
'
对我起了作用。下面是我用“
从临时注释数据库中删除的查询,其中注释标题='king Aj'和注释数据='ajinkya的用户名是clash royale中的king Aj'和注释日期时间='13/01/2020 07:24'
。但我怀疑的是,我插入了一次DB中的撇号,当我想删除条目时,我将用
'
(两次撇号)替换单个撇号在查询中,它是有效的。您能解释一下它是如何工作的吗?查询是如何理解用一个撇号删除条目的?
是自SQL-92创建以来克服
字符串限制器的最古老的技巧。现在有更有效的方法来实现这一点。我鼓励您研究所有这些方法并找出最有效的方法适合你的需要。
public class MainActivity extends AppCompatActivity {

    TextView NotesTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NotesTitle = this.findViewById(R.id.notesview);

        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        // Inserts into the database
        databaseHelper.insert(
                "When I put an apostrophe in the text <b>(e.g. Ajinkya's application)</b>, " + //<<<< BOLD ADDED
                        "the EditText data is not getting inserted into the database " +
                        "<i><strong>because it treats the first apostrophe as a query break</strong></>." //<<<< Italic String ADDED
        ); 
        // Extracts from the database and sets the notesview TextView with the extracted text from the HTML (if any)
        NotesTitle.setText(Html.fromHtml(databaseHelper.getHTML(1L)));
    }
}