yoav.str
yoav.str

Reputation: 1534

sql syntax how to write it down in java

i want to write this line in an sqlight db b:

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

therefore in my java file who generate this data base i am doing :

stat.executeUpdate("create table "+"android_metadata"+"("+"locale"+ " TEXT DEAFULT"+");");
    PreparedStatement prep1 = conn.prepareStatement(
              "insert into "+"android_metadata"+" values (?);");
    prep1.setString(1, "en_US") ;

but the output is not similer how do i make the sqlight locale be "locale" with "" in the declaration and the field locale would be just TEXT

as similer to this blog start : http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

thanX

Upvotes: 2

Views: 354

Answers (3)

Andreas Dolk
Andreas Dolk

Reputation: 114777

The java String equivalents are:

// CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
final static private String create = "CREATE TABLE \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')";

// INSERT INTO "android_metadata" VALUES ('en_US')
final static private String insert = "INSERT INTO \"android_metadata\" VALUES ('en_US')";

EDIT upgraded the code according to duffymos comment :)

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838216

To escape the double quotes use a backslash:

stat.executeUpdate("create table \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US');");

Also I think you don't need double quotes round android_metadata as it is not an SQL keyword.

Upvotes: 6

Blorgbeard
Blorgbeard

Reputation: 103467

You need to escape the quote symbol (and fix a typo - DEAFULT)

stat.executeUpdate("create table \"android_metadata\" (\"locale\" TEXT DEFAULT 'en-US');");

Upvotes: 3

Related Questions