How to access database from any of the programming language? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to access database from any of the programming language?

SQL is cool. But how will a program written in lets say Python will able to access data from a database

8th Oct 2016, 3:39 PM
Rohit Kumar
3 Answers
+ 2
The syntax for connecting to a database is different for each language but the idea is the same. There are many libraries that you can use to do this. First you have to request for a connection to the database, normally providing an address to where the database is and a username and password to authenticate to the database server. Once you are connected you can then query the database by providing it with an SQL string or calling stored procedures on the database. This will provide some sort of result back from the request which you can then use in your program. here is a python example: (it uses the MySQL python library, taken from stackoverflow) #!/usr/bin/python import MySQLdb # connect to an addres with a user name and password db = MySQLdb.connect(host="localhost", user="user", passwd="", db="onco") cursor = db.cursor() # execute SQL select statement cursor.execute("SELECT * FROM LOCATION") # commit your changes db.commit() # get the number of rows in the result set numrows = int(cursor.rowcount) # get and display one row at a time. for x in range(0,numrows): row = cursor.fetchone() print row[0], "-->", row[1] Hope it helps
9th Oct 2016, 9:33 AM
Andrew Dunk
Andrew Dunk - avatar
0
for this you can use eclipse IDE it's simple way to connect any language to db
11th Oct 2016, 6:24 PM
Kiran Borade
Kiran Borade - avatar
0
For Java,establishing connection with database and creating a table import java.sql.*; import java.io.*; public class DemoCreate { public static void main(String args[]) { Connection con; Statement statement; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:slink"); statement=con.createStatement(); statement.executeUpdate("Create table employee(empid number, ename text(10),designation text(10))"); System.out.println("Succssfully Created!"); statement.close(); con.close(); }catch(ClassNotFoundException ce){ System.out.println("Error 1: "+ ce.getMessage()); }catch(SQLException se){ System.out.println("Error 2: "+ se.getMessage()); } } }
12th Oct 2016, 5:36 PM
Sumita Das
Sumita Das - avatar