How to check if record exists with Python MySQdb

后端 未结 4 1512
深忆病人
深忆病人 2021-02-14 05:25

Im creating a python program that connects to mysql.

i need to check if a table contains the number 1 to show that it has connected successfully, this is my code thus fa

4条回答
  •  渐次进展
    2021-02-14 06:01

    I recently improved my efficiency by instead of querying select, just adding a primary index to the unique column and then adding it. MySQL will only add it if it doesn't exist.

    So instead of 2 statements:

     Query MySQL for exists:
         Query MySQL insert data
    

    Just do 1 and it will only work if it's unique:

    Query MySQL insert data
    

    1 Query is better than 2.

提交回复
热议问题