Insert data only if Table is empty in MySQL

  • Thread starter Thread starter iDesmet
  • Start date Start date
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
 
¤ 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)
 
¤ 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.
 
Back
Top