How to create Web sql using var declaraion?

Hi, experts how to correct this code? I declare S1 = Mytable. when excecute the code below S1 would be the result. I want the result would be Mytable.

    function create()
    {
        var S1 = "MyTable";
        db.transaction(function (tx)
        {
            tx.executeSql('CREATE TABLE IF NOT EXISTS S1 (s_id INTEGER PRIMARY KEY ASC, id unique, firstName, familyName)');
        });
        alert("Successfully created!");
    }
Whing Dela CruzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
firstly "S1" does NOT exist in MS SQL Server so the SQL cannot test for it (and SQL will only test for SQL Objects too)

This is SQL Server syntax for checking if a table exists:

if not exists (select * from sysobjects where name='MyTable' and xtype='U')
    create table MyTable (
        s_id INT IDENTITY(1,1) PRIMARY KEY
      , id -- type and length
      , firstName NVARCHAR(40) -- type and length
      , familyName NVARCHAR(100) -- type and length
    )
go

you also need to specify data types (and lengths if relevant)
0
chaauCommented:
@PaulMaxwell: Looks like it is not a MS SQL Server. It is something called Web SQL. It uses SQLite as a backbone. In SQLIte is perfectly normal to omit the data type and length.

To answer the OP's question: you need to it like this:
 function create()
    {
        var S1 = "MyTable";
        db.transaction(function (tx)
        {
            tx.executeSql('CREATE TABLE IF NOT EXISTS ' + S1 +' (s_id INTEGER PRIMARY KEY ASC, id unique, firstName, familyName)');
        });
        alert("Successfully created!");
    } 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Whing Dela CruzAuthor Commented:
Hi Paul Maxwell, the code above is already working on my pc. The only thing I need is that to replace "MyTable" into S1 since S1 was already declared as MyTable. There's no problem if make the code like this;
 
tx.executeSql('CREATE TABLE IF NOT EXISTS Mytable (s_id INTEGER PRIMARY KEY ASC, id unique, firstName, familyName)');

the thing I need is how to make the statement that would replace Mytable to S1 since S1 is variable. It is possible? thanks!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Whing Dela CruzAuthor Commented:
Hi chaau, your perfectly right! thanks both of you. God Bless!
0
Whing Dela CruzAuthor Commented:
Thanks Paul for reminding me!
0
PortletPaulfreelancerCommented:
no problem, it's all part of the service :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.