简体   繁体   English

将字符串转换为char数组

[英]Converting a string to char array

I couldn't find a perfect title for the problem but here it comes..Is there a way in which a string "hello" can be changed into a 'hello' in java (android)?? 我找不到这个问题的完美标题,但是它来了。.是否有一种方法可以在Java(android)中将字符串“ hello”更改为“ hello”? This is to make my sql query work. 这是为了使我的sql查询工作。 For example, here's my function written in java for an android app 例如,这是我用Java编写的用于Android应用程序的函数

public Cursor GetLecture(String course_code)
{
    SQLiteDatabase sqldb = this.getReadableDatabase();
    String query = "SELECT * FROM Lecture WHERE course_code =" + course_code;
    Cursor C = sqldb.rawQuery(query, null);
    return C;
}

The problem here is that what the query needs is for the value of the course_code to be for instance 'S11'and not "S11" which the variable course_code has as it is a string..So how can I change the string value which has double quote ("") to single quote ('') to make the query work? 这里的问题是查询所需要的是将course_code的值设置为例如“ S11”,而不是变量“ course_code”具有字符串的“ S11”。因此,如何更改具有双引号(“”)到单引号(“)使查询有效? Is there a shorter way or I have to change the whole algorithm?? 有没有更短的方法,或者我必须更改整个算法? Thanks in advance for considering!! 在此先感谢您的考虑!

Strings are sequence of characters delimited by double quotes. 字符串是由双引号分隔的字符序列。 You can't delimit them by single quotes. 您不能使用单引号将它们定界。 Those are for characters. 这些是为字符。 This one is in Java. 这是用Java编写的。

In case of SQL query, to enclose your string in single quotes, you can do it like this: 如果是SQL查询,可以将字符串用单引号引起来,您可以这样做:

String query = "SELECT * FROM Lecture WHERE course_code = '" + course_code + "'";

If you rewrite : 如果您重写:

String query = "SELECT * FROM Lecture WHERE course_code =" + course_code; 

as

String query = "SELECT * FROM Lecture WHERE course_code ='" + course_code + "'"; 

There is a special construction to make the values ​​in the query: 有一个特殊的构造可以在查询中生成值:

p_query = "select * from mutable where name_field = ?";
mDb.rawQuery(p_query, new String[] { fieldValue });

or you can simply add escape symbols and quotes 或者您可以简单地添加转义符号和引号

String query = "SELECT * FROM Lecture WHERE course_code = \"" + DatabaseUtils.sqlEscapeString(course_code) + "\"";

from here: 从这里:

Android quotes within an sql query string sql查询字符串中的Android引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM