Create MySQL database with python [closed]

2024/7/5 11:13:39

I want to execute a file with parameters like this : ./file.py server adress arg2 arg3 arg4

file1.py :

import MySQLdb# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() method
cursor = db.cursor()# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")# Fetch a single row using fetchone() method.
data = cursor.fetchone()print "Database version : %s " % data# disconnect from server
db.close()
Answer

Use the built-in module argparse. Or optparse if you're stuck on Python 2.6 or earlier.

https://en.xdnf.cn/q/120685.html

Related Q&A

Basic Python repetition exercise

I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. No…

Python 3.3 socket programming error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

ValueError: math domain error

I wrote this codedef partE():e = 3 * 10 // 3 + 10 % 3print("e).", e)partE()and python comes back with this error message when I try to run it. I do not understand why. Can someone please expl…

how to access objects in python with for loop in different files

This is my file1.json: {"count": 1,"next": null,"previous": null,"results": [{"id": 5883,"url": "https://some.api.com/api/ipam/ip-addres…

multiplicative digital root of a number using loops

I need to find the multiplicative digital root of a number in python using only loops.something that does the same as the below code but using loops:print("multiplicative digital root of a number …

How to access key values in a json files dictionaries with python

I have a script that pulls json data from an api, and I want it to then after pulling said data, decode and pick which tags to store into a db. Right now I just need to get the script to return specifi…

How to use sin(x) and cos(x) functions with eval

I need a program which can make graphs by matplotlib with functions I write in the console. But it doesnt work with trigonometric functions. The code I already wrote is:from numpy import linspace impo…

python - whats the difference between = and ==? [duplicate]

This question already has answers here:What do the symbols "=" and "==" mean in python? When is each used?(5 answers)Closed 5 years ago.I wonder know whats the difference between …

Sorting images by dates into a list from a dictionary

I know I asked this before, but Im still not sure why I just get an empty list when I test thisdef sorted_images(image_dict): (dict) -> list of strGiven an image dictionary return a list of the file…

multiple search and replace in python

I need to search in a parent folder all files that are config.xml and in those files replace one string in another. (from this-is to where-as)