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
  I have a syntax error! rohes_kaugummi 3 326 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 357 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 483 Jun-21-2021, 08:05 AM
Last Post: ibreeden
Exclamation getting a syntax error and i don't know why ilikedofs 10 762 May-13-2021, 08:52 PM
Last Post: deanhystad
  Even number code syntax error MrCeez 1 413 May-02-2021, 06:43 PM
Last Post: Larz60+
  Function syntax error Changosoyyo 2 558 Mar-07-2021, 10:42 PM
Last Post: Changosoyyo
  Syntax error on line 29 manwithjeans 3 614 Feb-27-2021, 08:28 AM
Last Post: perfringo
  Invalid syntax error - need help fixing calgk01 3 663 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Why do I get Syntax error? TechNitium 1 470 Jan-22-2021, 12:49 PM
Last Post: snippsat
  syntax error CompleteNewb 8 768 Jan-08-2021, 04:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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