Insert data only if Table is empty in MySQL

I

iDesmet

Hallo,

I'm creating a Wizard in VB.NET. In this wizard I establish the
connection to MySQL and then if it's successful then it creates (if
not exists) a database, tables, amoung others.

The question is, how do I insert data to a table only if it's empty.
Logically if it's not empty, then it shouldn't insert anything. I want
to add this in VB.NET code but don't know how to achive this.

To get the total rows I should use this: SELECT count(*) from db.table
t;

But as I had said, I don't know how to make the wizard know if the
table is empty or not.

Hope somebody can help me on this or show me the way.

Thanks in advance.

Regards,
iDesmet
 
P

Paul Clement

¤ Hallo,
¤
¤ I'm creating a Wizard in VB.NET. In this wizard I establish the
¤ connection to MySQL and then if it's successful then it creates (if
¤ not exists) a database, tables, amoung others.
¤
¤ The question is, how do I insert data to a table only if it's empty.
¤ Logically if it's not empty, then it shouldn't insert anything. I want
¤ to add this in VB.NET code but don't know how to achive this.
¤
¤ To get the total rows I should use this: SELECT count(*) from db.table
¤ t;
¤
¤ But as I had said, I don't know how to make the wizard know if the
¤ table is empty or not.
¤
¤ Hope somebody can help me on this or show me the way.

Try using the ExecuteScalar method to query the count:

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar.aspx


Paul
~~~~
Microsoft MVP (Visual Basic)
 
I

iDesmet

¤ Hallo,
¤
¤ I'm creating a Wizard in VB.NET. In this wizard I establish the
¤ connection to MySQL and then if it's successful then it creates (if
¤ not exists) a database, tables, amoung others.
¤
¤ The question is, how do I insert data to a table only if it's empty.
¤ Logically if it's not empty, then it shouldn't insert anything. I want
¤ to add this in VB.NET code but don't know how to achive this.
¤
¤ To get the total rows I should use this: SELECT count(*) from db.table
¤ t;
¤
¤ But as I had said, I don't know how to make the wizard know if the
¤ table is empty or not.
¤
¤ Hope somebody can help me on this or show me the way.

Try using the ExecuteScalar method to query the count:

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcomman...

Paul
~~~~
Microsoft MVP (Visual Basic)

Thanks, I figured it out how to do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top