Knowledgebase

كيفية استخدام البايثون للاتصال بقاعدة بيانات

  • 0

في هذا المثال، قمنا بإنشاء نص برمجي اختبار اسمه test_db.py ووضعه في مجلد cgi. كما قمنا بتحديث أذونات الملف إلى 755.

 

كما استخدمنا الإعدادات التالية ضمن الشفرة:

 

اسم قاعدة البيانات: inmoti6_pytest

مستخدم قاعدة البيانات: inmoti6_pytest

كلمة مرور قاعدة البيانات
قاعدة بيانات المضيف

نحن علي اتصال بقاعدة البيانات ليظهر لنا أي إصدار 

 
 
 
 

#!/usr/bin/env python

import MySQLdb

# connect to the database
db = MySQLdb.connect("localhost","inmoti6_pytest","pytest","inmoti6_pytest" )

# setup a cursor object using cursor() method
cursor = db.cursor()

# run an sql question
cursor.execute("SELECT VERSION()")

# grab one result
data = cursor.fetchone()

# begin printing data to the screen
print "Content-Type: text/html"

print

print """\
<html>
<head>
<title>Python - Hello World</title>
</head>
<body>
"""

print "Database version : %s " % data

print"""\
</body>
</html>
"""

# close the mysql database connection
db.close()

 

 

 


Was this answer helpful?