Look for existing entry in database before trying to insert - python

I am currently working with Access 2013. I have built a database that revolves around applicants submitting for a Job. The database is set up so that a person can apply for many different jobs, when the same person applies for a job through our website (uses JotForms) it automatically updates the database.
I have a Python script pulling the applicants submission information from an email which updates the database. The problem that I am running into is that within the database I have the applicants primary email set to "no duplicates", thus not allowing the same person to apply for many different jobs as the Python script is trying to create a new record within the database causing an error.
Within my Access form (VBA) or in Python what do I need to write to tell my database if the primary emails are the same only create a new record within the position applied for table that is related to the persons primary email?
Tables:
tblPerson_Information tblPosition_Applied_for
Personal_ID (PK) Position_ID
First_Name Position_Personal_ID (FK)
Last_Name Date_of_Submission
Clearance_Type
Primary_Phone
Primary_email
Education_Level

Simply look up the email address in the [tblPerson_Information] table:
primary_email = 'gord#example.com' # test data
crsr = conn.cursor()
sql = """\
SELECT Personal_ID FROM tblPerson_Information WHERE Primary_email=?
"""
crsr.execute(sql, (primary_email))
row = crsr.fetchone()
if row is not None:
personal_id = row[0]
print('Email found: tblPerson_Information.Personal_ID = {0}'.format(personal_id))
else:
print('Email not found in tblPerson_Information')

Related

How to input query flat output from a different database to a Django model?

First off, I'm new to Django and Python overall - so I might be asking the wrong question, if that's the case please tell me where to look. If not, continue:
I'm working with a Django app that queries a Wordpress database, I have created a connection and wrote the query as such:
cnxn = MySQLdb.connect(host="localhost",
user="root",
passwd="password",
db="dbname")
query = '''SELECT post_id, label, meta_value
FROM table'''
The issue is that the data is in a flat format, where the label should be the column name and the meta_value should be the value, output:
post_id label meta_value
------- ----- ----------
1 name example name
1 email example#mail.com
2 name example name 2
2 email example2#mail.com
Please keep in mind that there are 24 labels for each post_id, so each form entry adds 24 lines to the table.
How would I got about creating a Django model that appends the data from the table in a pivot style format to be able to display it nicely in the template? The goal is to be able to display 4-5 labels on the main page with a link on each line that shows the full input on a separate page.
Desired output:
post_id name email
------- ---- -----
1 example name example#mail.com
2 example name 2 example2#mail.com
Thank you!
I ended up converting the initial output to a pivot using Pandas, then uploaded the df into a new model that I created with the same exact field names.
def users(request):
df = pd.read_sql(query, cnxn)
pvt = df.pivot(columns='label', index='post_id', values='meta_value')
pvt = pvt.reset_index()
pvt['id'] = pvt.index
pvt.to_sql('table_name', cnxn, if_exists='replace')
The model configured includes all the fields in the queried table, so the outcome matched the model and I can treat it as an object.
Hope this helps.

Django Import-Export Import Duplicate key value violates Error

I'm working on a project with Django 1.10, Python 3.6 and PostgreSQL as the database backend, in which I have a model called 'Article" and I need to import data from CSV files. I have imported my first CSV file successfully with the following fields:
id, link & category
It's ID fields starts from 1 to 1960
then in next file, I have started ID field from 1961 to onward but it shows the following error:
Line number: 1961 - duplicate key value violates unique constraint "article_article_pkey" DETAIL: Key (id)=(1961) already exists.
Even when i see my Article model in Django admin it shows IDs from 1- 1960
Here's my models.py:
class Article(models.Model):
id = models.AutoField(primary_key=True)
link = models.URLField(max_length=255)
category = models.CharField(max_length=255, choices=Categories)
Here's admin.py
#admin.register(Article)
class ArticleAdmin(ImportExportModelAdmin):
resource_class = views.ArticleResource
readonly_fields = ('id',)
I have triggered that what's the issue :
Actually, the problem is PostgreSQL primary key sequential which is not synced with table rows.
That's why, when I insert a new row I get a duplicate key error because the sequence implied in the serial datatype returns a number that already exists.
To solve this issue we have to reset the ID sequential for PostgreSQL,
Here's the step by step guide:
Log onto the DB shell and connect to your database
First, check maximum value for id column of your table as SELECT MAX(id) FROM your_table;
Then, check what's going to be the next value for ID as : SELECT nextval('your_table_id_seq');
If nextval is the next number of Max value then it's right. e.g MAX=10 & nextval=11
Otherwise reset the id_seq for your table as:
BEGIN;
-- protect against concurrent inserts while you update the counter
LOCK TABLE your_table IN EXCLUSIVE MODE;
-- Update the sequence
SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false);
COMMIT;
I had this issue, because I'd specified a custom import_id_fields field but hadn't excluded the id field.
I had specified:
import_id_fields = ('code',)
skip_unchanged = True
report_skipped = True
But I needed:
import_id_fields = ('code',)
skip_unchanged = True
report_skipped = True
exclude = ('id',)
Bit of a noob error but this might save someone a google.
Ref: https://github.com/django-import-export/django-import-export/issues/273
i often run a for loop. This also updates my auto datetime fields:
import Article
for i in range(1, your_last_id + 1):
item = Article.objects.get(id=i)
item.save()
your_last_id may be SELECT MAX(id)

django db_table not changing

Hi i have created a dynamic table model and it is not changing whenever i select another table from the list i ve created in the template...
if i select the first table to see the data of it and then go back to the list and select the second table i get this error
InternalError: (1054, u"Unknown column 'table_name1.id' in 'field list'")
So, when I change from http://127.0.0.1:8000/tables/id=1 to http://127.0.0.1:8000/tables/id=2 it gives me the error.
But if I restart the server and go straight to
http://127.0.0.1:8000/tables/id=2 it works.
But now http://127.0.0.1:8000/tables/id=1 it doesn't work.
this is the party of views.py
def addview(request, pk):
table_name = Crawledtables.objects.get(id=pk)
print table_name
AllTables._meta.db_table = table_name.name
print AllTables._meta.db_table
tbl_detail = AllTables.objects.all()
print tbl_detail
return render(request, 'tables/table_list.html', {'details': tbl_detail})
The prints are just for testing and they work.
It prints out the correct table I select.
But the tbl_detail it doesn't print when i go to the 2nd table that i selected. only when I restart the server.
CrawledTables holds all the table names inside that DB with the date of creation of each table. I get the table name of the CrawledTables and put it in the AllTables._meta.db_table.
AllTables access all the tables in the DB ( same structure, different table names with different data inside of each table)
Do I need to clear the db_table everytime i go to the selected table?
I have done all the migrations correctly.
Please help me. Thank you in advance.

How to check if a table exists in Python?

I'm currently going over a course in Web design. What I want to do is to check if a table named portafolio exists in my database, if not I want to create one.
I'm using Python (flask) and sqlite3 to manage my database.
So far I the some of the logic in SQL to create a table if it doesn't exist:
# db is my database variable
db.execute('''create table if not exists portafolio(id INTEGER PRIMARY KEY AUTOINCREMENT,
stock TEXT,
shares INTEGER,
price FLOAT(2),
date TEXT
''');
But instead of using SQL commands I'd like to know how would I do the exact same checking in Python instead, since it would look a lot cleaner.
Any help would be appreciated.
Not sure about which way is cleaner but you can issue a simple select and handle the exception:
try:
cursor.execute("SELECT 1 FROM portafolio LIMIT 1;")
exists = True
except sqlite3.OperationalError as e:
message = e.args[0]
if message.startswith("no such table"):
print("Table 'portafolio' does not exist")
exists = False
else:
raise
Note that here we have to check what kind of OperationalError it is and, we have to have this "not pretty" message substring in a string check because there is currently no way to get the actual error code.
Or, a more SQLite specific approach:
table_name = "portafolio"
cursor.execute("""
SELECT name
FROM sqlite_master
WHERE type='table' AND name=?;
""", (table_name, ))
exists = bool(cursor.fetchone())
If you are looking for a neat job with Database operations, I advice you learn more about ORM(Object Relation Model).
I use Flask with SQLAlchemy. You can use python classes to manage SQL operations like this:
class Portafolio(db.Model):
id = db.Column(db.Integer, primary_key=True)
stock = db.Column(db.String(255), unique=True)
shares = db.Column(db.Integer, unique=True)
It does all the database checks and migration easily.

How to select and limit the related_name connection in the Peewee ORM?

I'm using Flask with the Peewee ORM in which I have defined two tables like so:
class Ticket(db.Model):
created = DateTimeField(default=datetime.now)
customer_uuid = CharField() # the customer's UUID gotten from App. More info comes from bunq API.
ticket_type = ForeignKeyField(TicketType, related_name='tickets')
active = BooleanField(default=True)
class Assign(db.Model):
created = DateTimeField(default=datetime.now)
ticket = ForeignKeyField(Ticket, related_name='assigned_to')
user = ForeignKeyField(User, related_name='assigned_tickets')
In the Assign table, several users can be assigned to a ticket, but only the last one counts (i.e., if a new user gets assigned, the previous ones should be disregarded). So I select the active tickets using the following:
open_tickets = Ticket.select().where(Ticket.active == True)
I now want to use this loop in my template. With every iteration however, I also want to display the assigned user. But open_ticket[0].assigned_to obviously returns several assignments, and with it several users.
Would anybody know how I can get the latest assigned user for every ticket within a loop?
This worked for me in Sqlite:
q = (Ticket
.select(Ticket, Assign, User)
.join(Assign)
.join(User)
.group_by(Ticket)
.order_by(Ticket.id, Assign.id.desc()))
for ticket in q:
print ticket.id, ticket.assign.user.username

Categories