Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, wouldn't it be better to avoid having the sql connection inside the <code>__init__</code>? You will have a problem if you want to use this class in the same instance after using <code>__fina__</code>. You could have it in another method and call it and call the connection closing method when needed and commit after each method is executed.</p> <p>Here is what I use : Create a class method that connects to the db and executes a query from an argument,commits and closes connection and you pass any query you want executed as an argument of that method.You can simply call this method anytime you want. And the best about this is that you can create a method that passes multiple queries as arguments before closing db connection. This is specially usefull if have to use sql connections to the same db in another class without using a set of methods each time you need to execute a sql query.</p> <p>Here is a little example I used with MySQLdb module, It's pretty simple but it worked.</p> <pre><code>import MySQLdb class DbQuery: '''Here is the class I talked about''' def __init__(self): '''You can define the main queries here but it's not necessary They can be global variables If you don't have class dependency from which you get variables you might not even need to define __init__''' def Sql_Connect(self): self.db = MySQLdb.connect("localhost","root","","data_db" ) self.cursor = db.cursor() def Sql_Commit(self): self.db.commit() print "Info : Database updated" except: self.db.rollback() print "Error : Database rollback" self.db.close() def Query(self,query): self.Sql_Connect() try : self.cursor.execute(query) self.Sql_Commit() </code></pre> <p>The only thing important is to remember the query structure.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload