I have a problem that, in the bash profile, I was doing select 2 columns from a table and filtering all data which the framework column value is java into testcases file. But I want to use that python, I tried a lot of ways but without any success.
echo "select class, testcase from test_cases where framework = 'Java';" | mysql -h vm.dev.midasplayer.com --ssl-mode=DISABLED -u ${DB_USER} -p${DB_PASSWORD} tabletest > testcases
I try using the following python to connect the DB but then how to perform the SQL query and then import it into a file? Any help here will be much appreciated
import mysql.connector
mydb = mysql.connector.connect(
host="vm.dev.midasplayer.com",
user="username",
password="password"
)
print(mydb)
CodePudding user response:
What you need is the most basic and elementary for backend developers who start to use a relational database. You wil find a lot if information in the internet with these google searches:
- simple mysql crud with [java, python, c#, nodejs, etc]
- simple mysql select with [java, python, c#, nodejs, etc]
Also you can add at the end of google search the following commands to get pdfs or ppts used in thousands of courses of all the world :)
- simple mysql select with python filetype:ppt
- simple mysql select with python filetype:pdf
Learning mode
If you have time to learn steps by step I recommend to read carefully all the information found with the recommended searches in the previous section
I share here some useful pdfs
- https://www.google.com/search?q=python mysql filetype:pdf&client=ubuntu&hs=1SN&channel=fs&sxsrf=AJOqlzUQ95ilbcWWZpmZXoNsQND7eLMl8g:1674408020105&ei=VHDNY86RBuXH5OUPkfaV0AY&start=30&sa=N&ved=2ahUKEwjOi_3519v8AhXlI7kGHRF7BWo4FBDw0wN6BAgFEBc&biw=1149&bih=683&dpr=1
- https://downloads.mysql.com/docs/connector-python-en.a4.pdf
- https://www.tutorialspoint.com/python_mysql/python_mysql_tutorial.pdf
- https://no2imphal.kvs.ac.in/sites/default/files/Chapter 10 - Interface Python with MySQL.pdf
- https://isip.piconepress.com/courses/temple/ece_3822/resources/tutorials/python/python_database_access.pdf
- http://python.mykvs.in/uploads/tutorials/XIIComp.Sc.34.pdf
- https://www.cs.columbia.edu/~hgs/teaching/ap/examples/scripts_python_dbapi.pdf
- https://ictswami.files.wordpress.com/2019/10/python-mysql-database.pdf
- https://labdeck.com/examples/python/py-db3/table-create-mysql-python.pdf
- https://pdfsecret.com/download/a-step-by-step-guide-to-using-mysql-with-python-this-tutorial-will-help-you-set-up-a-mysql-connection-from-a-python-program-we-assume-you-al-_59fa6f74d64ab28ae25e009a_pdf
- https://www.ijsr.net/archive/v8i2/ART20194929.pdf
- https://theswissbay.ch/pdf/Gentoomen Library/Programming/Python/MySQL for Python (2010).pdf
If links in the future are offline, check this https://github.com/open-source-education/junior-software-developer/tree/main/jsd-306
If you don't have time to learn
Not recommended but if you don't have time to learn, you could use https://gist.github.com to search a code ready to use
pre-requisites
pip install mysql-connector
pip install mysql-connector-python-rf
code
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
try:
connection = mysql.connector.connect(host='localhost',
database='devopsschool1',
user='root',
password='MyNewPass')
sql_select_Query = "select * from Laptop"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
print("Total number of rows in Laptop is: ", cursor.rowcount)
print("\nPrinting each laptop record")
for row in records:
print("Id = ", row[0], )
print("Name = ", row[1])
print("Price = ", row[2])
print("Purchase date = ", row[3], "\n")
except Error as e:
print("Error reading data from MySQL table", e)
finally:
if (connection.is_connected()):
connection.close()
cursor.close()
print("MySQL connection is closed")
Snippet source: https://gist.github.com/devops-school/eab2f34e05145b9227ba413b1aa42da7#file-read-py