# Authenticate Username Against Database

**URL:** <https://forum.inductiveautomation.com/t/authenticate-username-against-database/4488>\
**Category:** Ignition\
**Created:** [May 17, 2012, 1:33pm UTC](https://forum.inductiveautomation.com/t/authenticate-username-against-database/4488 "2012-05-17T13:33:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlr573](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jlr573](https://forum.inductiveautomation.com/u/jlr573)\
**Post date:** [May 17, 2012, 1:33pm UTC](https://forum.inductiveautomation.com/t/authenticate-username-against-database/4488/1 "2012-05-17T13:33:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![diat150](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@diat150](https://forum.inductiveautomation.com/u/diat150)\
**Post date:** [May 17, 2012, 2:07pm UTC](https://forum.inductiveautomation.com/t/authenticate-username-against-database/4488/2 "2012-05-17T14:07:53Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![Greg.Buehler](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Greg.Buehler](https://forum.inductiveautomation.com/u/Greg.Buehler)\
**Post date:** [May 17, 2012, 2:14pm UTC](https://forum.inductiveautomation.com/t/authenticate-username-against-database/4488/3 "2012-05-17T14:14:43Z")

</div>

[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:

```auto
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])
```
