Sven Ronnlund
Sven Ronnlund

Reputation: 137

Insert special characters into table name

I have an Android application. When I create a new table, if I add a special character like ! or $, the application crashes. I have created the functionality to replace an spaces with an _ like so:

String name = name.getText().toString().replace(" ", "_");

// execute my insert query here

How can I do the same for any other special character other than space when I create a new table? Is this possible and if so, how?

Upvotes: 2

Views: 325

Answers (2)

Santhosh
Santhosh

Reputation: 3991

You can escape special characters in a string using an inbuilt function in Android. Like below,

String name = DatabaseUtils.sqlEscapeString(name.getText().toString())

Upvotes: 3

Amy
Amy

Reputation: 4032

Use This:

String name = "Table$Name";
name = name.replaceAll("\\W", "_");

Upvotes: 3

Related Questions