-1

I have written the following short and simple script in order to connect a MySQL database called mybase with Python. I have populated already the table users of my database with data in MySQL Workbench. The problem is that when I run the script, I see no results printed in the console, but only my cmd opening for one second and closing automatically. Could someone help me find out what I am doing wrong? This is my script:

import mysql.connector as mysql


db = mysql.connect(host='localhost',
                   database='mybase',
                   user='root',
                   password='xxx',
                   port= 3306)


cursor = db.cursor()
q= "SELECT*FROM users"
cursor.execute(q)

for row in cursor.fetchall():
    print (row[0])

I appreciate any help you can provide!

Shadow
  • 33,525
  • 10
  • 51
  • 64
marialadelbario
  • 325
  • 1
  • 4
  • 19
  • 1
    You should first open your CMD and then run your script using `python mysript.py`. That way, you will see the results or errors in the console. It is normal that in your case, the CMD closes, since your script will finish after the for loop, hence close the your console. – Zwepp May 20 '19 at 14:15
  • @Zwepp I am using the built-in terminal of vs code – marialadelbario May 20 '19 at 14:18
  • Possible duplicate of [How to keep a Python script output window open?](https://stackoverflow.com/questions/1000900/how-to-keep-a-python-script-output-window-open) – wwii May 20 '19 at 14:27

1 Answers1

0

Maybe its a syntax error with your query. It should be like this to work

q= "SELECT * FROM users"

If it does not work, what I found helps me it is to test the queries first in a client with a local database and then copying them into my code.

rokjoana
  • 41
  • 5