check the Table if exist

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Helo,

can i checked the table, if the exist with follow code

B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS" & Name & ")

gruß
sinan
 

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
helo,

I would only check if the table exists, if not, then I want to create it.
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS" & ayir2(0) & ")
SQL1.ExecNonQuery("CREATE TABLE"  & ayir2(0) & "(Nummer TEXT , Name TEXT, Mesaj TEXT , LinkAdres TEXT, Datum TEXT)")
SQL1.ExecNonQuery2("INSERT INTO"  & ayir2(0) & "VALUES(?,?,?,?,?)", Array As Object(ayir2(0), ayir2(1), ayir2(2), ayir2(3),ayir2(4)))

but doesn't work
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Replace this line:
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS" & ayir2(0) & ")
with this line:
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS " & ayir2(0))
Notice the space after EXISTS

EDIT: I forgot to mention also that you need a space here after TABLE like this: ("CREATE TABLE "
Also, you need a space after INTO like this: "INSERT INTO "
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I'm trying to understand... You want to delete a table and recreate it, or you want to create it in case it doesn't exist? If it's the second, you should use
B4X:
create table if not exists yourTable (and so on...)
SQLite Query Language: CREATE TABLE
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Replace this line:
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS" & ayir2(0) & ")
with this line:
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS " & ayir2(0))
Notice the space after EXISTS

EDIT: I forgot to mention also that you need a space here after TABLE like this: ("CREATE TABLE "
Also, you need a space after INTO like this: "INSERT INTO "

only after TABLE ?
and here & ayir2(0) & " VALUES
between & and values?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Sinan: Here is the code with all the necessary spaces and syntax:
B4X:
SQL1.ExecNonQuery("DROP TABLE IF EXISTS " & ayir2(0))
SQL1.ExecNonQuery("CREATE TABLE "  & ayir2(0) & " (Nummer TEXT , Name TEXT, Mesaj TEXT , LinkAdres TEXT, Datum TEXT)")
SQL1.ExecNonQuery2("INSERT INTO "  & ayir2(0) & " VALUES(?,?,?,?,?)", Array As Object(ayir2(0), ayir2(1), ayir2(2), ayir2(3),ayir2(4)))
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
you can use
B4X:
if(table_name.compareto(SQ.ExecQuerySingleResult("Select name From sqlite_master WHERE Type='table' AND name='"& table_name & "'"))==0)then
log("Table exist")
else
log("Table don't exist")
end if
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
you can use
B4X:
if(table_name.compareto(SQ.ExecQuerySingleResult("Select name From sqlite_master WHERE Type='table' AND name='"& table_name & "'"))==0)then
log("Table exist")
else
log("Table don't exist")
end if


Helo,

this code doesn't working
 
Upvote 0

madSac

Active Member
Licensed User
Longtime User
table_name is name of table .....
B4X:
SQ.ExecQuerySingleResult("Select name From sqlite_master WHERE Type='table' AND name='"& table_name & "'")
this code returns table name if table exist otherwise it returns null.
 
Upvote 0
Top