I need to know how to query a table in sql server. Everywhere i look it says to use pyodbc but they didn't make one for 3.4 .
-
What kind of connection are you trying to make? SQLite / MySQL / MsSQL?Celeo– Celeo2014年09月09日 21:55:27 +00:00Commented Sep 9, 2014 at 21:55
-
In Python 3.4 you can use the pyodbc module which is labeled for use with Python 3.3mechanical_meat– mechanical_meat2014年09月09日 22:07:21 +00:00Commented Sep 9, 2014 at 22:07
-
@Celeo, it clearly says for "SQL Server"...it's a Microsoft product.Andy– Andy ♦2014年09月09日 22:25:03 +00:00Commented Sep 9, 2014 at 22:25
1 Answer 1
There isn't an official pyodbc for Python 3.4. However, as bernie mentions in his comment, you can try the 3.3 version. Additionally, the University of California, Irvine provides an unofficial build for pyodbc for Python 3.4. There is also the pypyodbc package, as an option.
In any case, once you decide on which to utilize, you can use it by doing something like this. Realize this a very simple query.
import pyodbc
# Create connection
con = pyodbc.connect(driver="{SQL Server}",server=SERVERNAME,database=DATA_BASE_INFO,uid=username,pwd=password)
cur = con.cursor()
db_cmd = "SELECT * FROM table_name"
res = cur.execute(db_cmd)
# Do something with your result set, for example print out all the results:
for r in res:
print r
In the line that beings con =, there are several values for you to fill in.
serverneeds to be server host information (either IP or DNS name), and possibly a port (default is 1435)"dns.to.host.com,1435"uidis the username you are logging in withpwdis the password of the user you are logging in with