I have created database and table on it with information about software versions, url for seeking updates, etc. I need to get info from this database by python. I have created procedure:
def getserverver(name):
connection = pymysql.connect(host='localhost',
user='root',
password='mysql',
db='softupdates',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
sql = "SELECT Name FROM softupdates.soft where Name like name"
cursor.execute(sql)
result = cursor.fetchone()
return result
And use it after:
panorama12onserver = (getserverver('Профессиональная ГИС %Панорама %версия 12%x64%'))['Name']
panorama11onserver = (getserverver('Профессиональная ГИС %Карта 2011%'))['Name']
panedit11onserver = (getserverver('Профессиональный векторизатор %Панорама-редактор%версия 11%'))['Name']
panedit12onserver = (getserverver('Профессиональный векторизатор %Панорама-редактор%версия 12%x64%'))['Name']
print (panorama12onserver)
print (panorama11onserver)
print (panedit12onserver)
print (panedit11onserver)
Why did i get the result like this:
Профессиональная ГИС "Карта 2011" (версия 11.13.5.7)
Профессиональная ГИС "Карта 2011" (версия 11.13.5.7)
Профессиональная ГИС "Карта 2011" (версия 11.13.5.7)
Профессиональная ГИС "Карта 2011" (версия 11.13.5.7)
The same data on every string, but it is not in database like this. What am I doing wrong (besides stripting =))?
Beause you are executing the same query over and over again. Try changing to
sql = "SELECT Name FROM softupdates.soft where Name like %s"
cursor.execute(sql, (name,))
And you don't need to open a connection every time inside getserverver
open it once (outside that function scope) and reuse it. Opening a database connection is a costly operation.
having fixed the original problem with this code, if you are worndering why you are not seeing a dictionary, well that's not how you are printing it!!
(getserverver('Профессиональный векторизатор %Панорама-редактор%версия 12%x64%'))['Name']
This returns a scalar value and not a dictionary.