Authenticate Username Against Database

I’m looking for a way to authenticate a username against the database once it’s created.

So it would work something like this:

  • once the user creates their username and password
  • click save
  • Ignition runs a query against the database
  • finds username already exist
  • open messageBox saying “sorry but the username already exist”

I know I can set the username column in the database to be unique but that returns an ugly error and not something I would want the end user to see.

if you look at the example user add edit window that is available you will see how to do it… but it goes something like this:

probe = system.db.runPrepQuery("SELECT username FROM users_db WHERE username = ?", [username], datasource) if probe != None and len(probe) > 0: system.gui.errorBox("User '%s' already exists" % username)

[quote=“jlr573”]I’m looking for a way to authenticate a username against the database once it’s created.

So it would work something like this:

  • once the user creates their username and password
  • click save
  • Ignition runs a query against the database
  • finds username already exist
  • open messageBox saying “sorry but the username already exist”

I know I can set the username column in the database to be unique but that returns an ugly error and not something I would want the end user to see.[/quote]

on the save button, you’d run something like:

username = event.source.parent.getComponent('txtUsername').text
password = event.source.parent.getComponent('txtPassword').text
exists = (system.db.runScalarQuery("SELECT Count(*) FROM users WHERE username LIKE '%s'" % username) > 0)

if exists:
	event.source.parent.getComponent('lblAlert').text = "Sorry, but the username %s already exists!" % username
else:
	system.db.runPrepUpdate("INSERT INTO USERS (Username, Password) VALUES (?,?)", [username, password])