0

More than once I've exported column names from a SQL script, e.g.

SELECT a, b, c from MyTable

to use in a list in python

MyColumns = ['a', 'b', 'c']

The embuggerance is adding the quotes when there are many columns. Currently I paste the names into Excel, do text to columns, transpose and use formulas to add the quotes, then paste back into code.

Is there a better way?

3
  • what sql are you using. there are many ways.. What you have described is possibly the hardest, most long winded way I have heard of! Commented Apr 4, 2018 at 0:30
  • It is just SQL script, so I treat it as text. Since you ask - at the moment I'm using a jupyter notebook to connect to MariaDB, so the SQL scripts are being run from jupyter. I'm also using HeidiSQL to connect. Commented Apr 4, 2018 at 0:37
  • i have updated my answer for mariadb.. according to their documentation.. its a MYSQL DB Commented Apr 4, 2018 at 0:40

1 Answer 1

1

Here is a way you would do it using python using mariadb.

Change the user, password and database name.

import mysql.connector as mariadb

# enter the credentials
mariadb_connection = mariadb.connect(user='python_user', password='some_pass', database='some_database')

# create a cursor to access the DB
cursor = mariadb_connection.cursor()

# Create an empty list to append queries to
columns = []

# loop over the desired columns, appending each one to the list
for column in cursor.execute("SELECT a, b, c from MyTable"):
    columns.append(column)

# check the output    
print(columns)

You can do any SQL query you like by putting what you would normally do inside the cursor.execute() command

if you just want to INSERT or CREATE you can do something like..

create a table

cursor.execute("""CREATE TABLE albums
                  (title text, artist text, release_date text, 
                  publisher text, media_type text) 
              """)

or

insert some data

cursor.execute("INSERT INTO albums VALUES ('Glow', 'Andy Hunter', '7/24/2012', 'Xplore Records', 'MP3')")

# save data to database
conn.commit()

Here is a link to the mariadb documentation for python..

https://mariadb.com/resources/blog/how-connect-python-programs-mariadb

If you wish to add sql data to a list from a string you can implement the following..

some_sql_string = "a b c john ashu jimmy"

lst = []

for x in some_sql_string.split():
    lst.append(x)

print(lst)

['a', 'b', 'c', 'john', 'ashu', 'jimmy']
Sign up to request clarification or add additional context in comments.

6 Comments

Do you have any suggestion if I cannot execute the SQL and still want to code?
I'm not sure what you mean.. can you give an example?
With the SQL script I can copy paste and do the long winded way in Excel without relying on having the database connection. So the example is - what to do if the database is not connected? As it seems I can only print(columns) if the cursor connects and does the execution.
you mean save the sql query to an excel file?
i have updated my answer.. if that is incorrect, please post a new question and put a link here and i will answer it better :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.