How to One to One relation sqlite3 python - python

This is the relation I want to create for my quiz app and im using python
Module:
c.execute("""CREATE TABLE Questions (
mod_id INTEGER PRIMARY KEY,
mod_name TEXT NOT NULL
)""")
Question:
c.execute("""CREATE TABLE Questions (
quest_id INTEGER PRIMARY KEY,
quest_name TEXT NOT NULL,
mod_id INTEGER NOT NULL,
FOREIGN KEY (mod_id)
REFERENCES Modules (mod_id)
)""")
Now Since the each feedback correspont to 1 question, how do i sort of create one to one relationship? should I do the same as I did for the question table? would this be handle automatically by sqlite3??
Appreciate all sort of help Thanks!!!
EDIT: Does my feedback table need to have a primary key?? I think no because when accesing the feeback table to delete or add data we just pass the question ID as the parameter to look that row up.

Related

How do I create a foreign key in sqlite3 python

I can't get my program to use a foreign key for some unbeknown reason. I have read other questions related to this but cant find an answer that works. I tried the PRAGMA foreign_keys = ON, but instead i get a syntax error for "foreign_keys". I also tried removing this and running the program again and instead got an error saying the column I'm trying to create doesn't exist
import sqlite3
import os
import random
from tkinter import *
import uuid
PRAGMA foreign_keys = ON
conn = sqlite3.connect('MyComputerScience.db')
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS users (
UserID text PRIMARY KEY,
FName text,
SName text,
username text,
password varchar,
userType text)""")
c.execute("""CREATE TABLE IF NOT EXISTS classes (
ClassID PRIMARY KEY,
FOREIGN KEY (User) REFERENCES users(UserID)))""")
FOREIGN KEY (User) REFERENCES users(UserID)))""")
sqlite3.OperationalError: unknown column "User" in foreign key definition
You need to create the fields in the table before using them as a foreign key.
As you did not add a field User to your table, you got the error:
unknown column "User" in foreign key definition
To fix the error, add User to your table definition
c.execute("""CREATE TABLE IF NOT EXISTS classes (
ClassID PRIMARY KEY,
User text,
FOREIGN KEY (User) REFERENCES users(UserID)))""")
However, I would question the design. The table classes would only be able to relate one user per class. Is that what you want to do?
c.execute("""CREATE TABLE IF NOT EXISTS classes (
ClassID PRIMARY KEY,
FOREIGN KEY (ClassID) REFERENCES users(UserID)))""")
That's the correct syntax but this way you set as foreign key your primary key. Maybe you wanted to use another field.

How to fix syntax error in SQLite in Python

I've finished making some SQLite tables and am executing the instructions. When executing the instructions, the following error has come up:
sqlite3.OperationalError: near "Category": syntax error
Most of my tables use the same sort of format, below is an example of one such table.
CategoryTableSQL = """ CREATE TABLE IF NOT EXISTS Category(
CategoryID integer PRIMARY KEY AUTOINCREMENT
Category text NOT NULL
);"""
databaseNewTable(Connection, CategoryTableSQL)
You forgot a comma between the declaration of your table fields in your SQL-statement: It should be like this. Always use comma's to seperate the field-creation statements. Except of course, for the last field you create =). Also, I would take care in naming your fields the same name as your tables. To prevent confusion. Just my two cents
CREATE TABLE IF NOT EXISTS Category(
CategoryID integer PRIMARY KEY AUTOINCREMENT,
Category text NOT NULL
);
I think, You just need to add the ',' symbol after the AUTOINCREMENT word. :-)
Like this:
CategoryTableSQL = """ CREATE TABLE IF NOT EXISTS Category(
CategoryID integer PRIMARY KEY AUTOINCREMENT,
Category text NOT NULL
);"""
databaseNewTable(Connection, CategoryTableSQL)

SQL for creating tables in a database using Python sqlite3

I am trying to create a number of tables with different names (of course) but sharing the same schema. For this purpose, I am using executemany on a Cursor object as follows:
tables = ['Meanings', 'Synonyms', 'Antonyms', 'Examples', 'Phrases',
'Pronunciations', 'Hyphenations']
create_table_query = '''CREATE TABLE (?) (
id INTEGER NOT NULL,
text TEXT,
word_id INTEGER NOT NULL,
PRIMARY KEY id,
FOREIGN KEY(word_id) REFERENCES Word(id)
)'''
cursor.executemany(create_table_query, tables)
When I execute this snippet, I get the following error message:
OperationalError: near "(": syntax error
I am having trouble fixing the bug here with my SQL since I find the error message to be not descriptive enough. I have tried the following queries but I am unable to develop the understanding of their success and my query's failure:
create_table_query_1 = '''CREATE TABLE {} (
id INTEGER NOT NULL,
text TEXT,
word_id INTEGER NOT NULL,
PRIMARY KEY id,
FOREIGN KEY(word_id) REFERENCES Word(id)
)''' # Syntax error near "id"
create_table_query_2 = '''CREATE TABLE (?) (
id INTEGER PRIMARY KEY,
text TEXT,
word_id INTEGER NOT NULL,
FOREIGN KEY(word_id) REFERENCES Word(id)
)''' # Syntax error near "("
create_table_query_1 = '''CREATE TABLE {} (
id INTEGER PRIMARY KEY,
text TEXT,
word_id INTEGER NOT NULL,
FOREIGN KEY(word_id) REFERENCES Word(id)
)''' # works with string formatting
Also, what are other efficient(in terms of time) ways to achieve the same?
To put my comment into an answer and expand on it: you cannot parametrize table nor column names. I was unable to find any documentation on this...
In a couple of the other examples you have extra parens/brackets that SQLite doesn't need.
So the solution, as you've found, is to use string substitution for the table names as in your final example.
Here's an example with a loop over all of your tables:
for table in tables:
cursor.execute('''CREATE TABLE {} (
id INTEGER PRIMARY KEY,
text TEXT,
word_id INTEGER NOT NULL,
FOREIGN KEY(word_id) REFERENCES Word(id)
)'''.format(table))
I am not completely clear on why you want different tables for the different types of words, though, as this would seem to go against the principles of database design.

sqlite 3 column does not exist, even when it's been created

I've created a table inside a database, with a column called 'recipeName'. After committing the newly created table to the database, it comes up with this error:
OperationalError: table recipes has no column named recipeName
Here's the code for creating the table, and for entering the stuff into the database...
cursor.execute("""CREATE TABLE IF NOT EXISTS \
recipes('id INT PRIMARY KEY AUTO_INCREMENT, recipeName')""")
#Save data to database
conn.commit()
cursor.execute("""INSERT INTO recipes(recipeName) VALUES('%s')""" % (recipeName))
I'll be really appreciative of any help I get... this is confusing me quite a lot... I've been working it for days now, and I haven't been able to find a fix...
Thanks in advance :)
Ryan :)
cursor.execute("CREATE TABLE IF NOT EXISTS recipes([id] INTEGER PRIMARY KEY AUTOINCREMENT, [recipeName])")
you only had one column named : 'id INT PRIMARY KEY AUTO_INCREMENT, recipeName'
also in sqlite you need to use INTEGER PRIMARY KEY AUTOINCREMENT (not INT and AUTO_INCREMENT)

Adding primary key to existing MySQL table in alembic

I am trying to add an 'id' primary key column to an already existing MySQL table using alembic. I tried the following...
op.add_column('mytable', sa.Column('id', sa.Integer(), nullable=False))
op.alter_column('mytable', 'id', autoincrement=True, existing_type=sa.Integer(), existing_server_default=False, existing_nullable=False)
but got the following error
sqlalchemy.exc.OperationalError: (OperationalError) (1075, 'Incorrect table definition; there can be only one auto column and it must be defined as a key') 'ALTER TABLE mytable CHANGE id id INTEGER NOT NULL AUTO_INCREMENT' ()
looks like the sql statement generated by alembic did not add PRIMARY KEY at the end of the alter statement. Could I have missed some settings?
Thanks in advance!
I spent some time digging through the alembic source code, and this doesn't seem to be supported. You can specify primary keys when creating a table, but not when adding columns. In fact, it specifically checks and won't let you (link to source):
# from alembic.operations.toimpl.add_column, line 132
for constraint in t.constraints:
if not isinstance(constraint, sa_schema.PrimaryKeyConstraint):
operations.impl.add_constraint(constraint)
I looked around, and adding a primary key to an existing table may result in unspecified behavior - primary keys aren't supposed to be null, so your engine may or may not create primary keys for existing rows. See this SO discussion for more info: Insert auto increment primary key to existing table
I'd just run the alter query directly, and create primary keys if you need to.
op.execute("ALTER TABLE mytable ADD id INT PRIMARY KEY AUTO_INCREMENT;")
If you really need cross-engine compatibility, the big hammer would be to (1) create a new table identical to the old one with a primary key, (2) migrate all your data, (3)delete the old table and (4) rename the new table.
Hope that helps.
You have to remove the primary key that is in the table and then create a new one that includes all columns that you want as the primary key.
eg. In psql use \d <table name> to define the schema, then check the primary key constraint.
Indexes:
"enrollments_pkey" PRIMARY KEY, btree (se_crs_id, se_std_id)
then use this information in alembic
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('enrollments', sa.Column(
'se_semester', sa.String(length=30), nullable=False))
op.drop_constraint('enrollments_pkey', 'enrollments', type_='primary')
op.create_primary_key('enrollments_pkey', 'enrollments', [
'se_std_id', 'se_crs_id', 'se_semester'])
The results after running \d enrollments should be updated to
Indexes:
"enrollments_pkey" PRIMARY KEY, btree (se_std_id, se_crs_id, se_semester)
This solution worked fine for me.

Categories

Resources