Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in your SQL syntax
#1
Hi,

I am wanting to check if a username already exists. If so a message box will be displayed, if not the username and the rest of the field entries will be stored in the database. However when I tried this, it prints database can’t connect. I tried this without line 65-69 and it works perfectly.
Reply
#2
Is this a GUI question or a database question?
Please show code and a full error traceback that reflects the error.
Reply
#3
(May-02-2021, 12:06 PM)Yoriz Wrote: Is this a GUI question or a database question?
Please show code and a full error traceback that reflects the error.

How do I post an image here?
Reply
#4
Please do not paste an image of your code.
Please read the following threads.
How to ask Smart Questions | What to include in a post | What to NOT include in a post
Reply
#5
// Deleted
Yoriz write May-02-2021, 12:15 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#6
// Deleted
Reply
#7
  def register(self):

        if self.name.get() == "" or self.username.get() == "" or self.password.get() == "":
            messagebox.showwarning("Alert", "All fields are required!")
        elif len(self.username.get()) < 3 or len(self.username.get()) > 25:
            messagebox.showwarning("Alert", "Username must be between 3 and 25 characters!")
        elif len(self.password.get()) < 6 or len(self.password.get()) > 25:
            messagebox.showwarning("Alert", "Password must be between 6 and 25 characters!")

        else:
            try:
                db = mysql.connector.connect(
                    host="localhost",
                    username="dbuser01",
                    password="computing",
                    database="geography_quiz"
                )

                db_cursor = db.cursor()
                db_cursor.execute("SELECT * FROM user_information WHERE Username=%s", self.username.get())
                row = db_cursor.fetchone()
                if row is not None:
                    messagebox.showerror("Error", "Username already exists!")
                else:
                    db_cursor.execute("INSERT INTO user_information (Full_name, Username, Password) values(%s, %s, %s)",
                                      (self.name.get(),
                                       self.username.get(),
                                       self.password.get()
                                       ))
                    db.commit()
                    db.close()
                    messagebox.showinfo("Registration Success!", "Welcome!")

            except:
                print("Cannot connect to database")
Reply
#8
By not checking for a particular exception you have no idea what went wrong
add a raise so you can find out what the exception is
...
...
...
except:
    print("Cannot connect to database")
    raise
then add that add code to deal with that particular exception or fix the unintended error.
Reply
#9
Output:

Cannot connect to database
Cannot connect to database
Reply
#10
Thanks

When I tried this it says

No connection could be made because the target machine actively refused it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,154 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 371 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,542 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,205 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,288 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,241 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 881 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,828 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,344 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,949 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020