Python Forum
Need to change connection and queries from SQLite to MySQL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to change connection and queries from SQLite to MySQL
#1
I am just starting to learn python and I am trying to figure this one app out. I am trying to change the code so my app connects to MySQL instead of SQLite and rewrite the queries because I know they are a bit different. Can someone help with this. I have uploaded a copy of the document. Thanks.

Jhon

Attached Files

.py   App_Stable.py (Size: 66.61 KB / Downloads: 46)
Reply
#2
Hi,

(Oct-20-2025, 03:08 PM)Bxapocalypse1 Wrote: Can someone help with this.

What did you try yet, what possibly didn't work? It will be quite a bit of work do the number of queries in the code.

Are you familiar with SQLite and MySQL? Are you familiar with the data types SQLite and MySQL offer? Any preference which Python module for MySQL to use? There are a few out there?

Regards, noisefloor
Reply
#3
My suggestion: login to your mysql database and try out the queries in your code. If you get an error, amend the code.

I tried the first 3 CREATE TABLE queries, below is what I got and what I changed. Number 3 worked 'as is' in mysql.

Quote:# doesn't work in mysql can't set TEXT UNIQUE get #1170 - BLOB/TEXT column 'class_name' used in key specification without a key length
CREATE TABLE IF NOT EXISTS classes (id INTEGER PRIMARY KEY, class_name TEXT UNIQUE NOT NULL)
# change to
CREATE TABLE IF NOT EXISTS classes (id INTEGER PRIMARY KEY, class_name VARCHAR(256) UNIQUE NOT NULL)

# ditto the above
CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT UNIQUE NOT NULL, password TEXT NOT NULL, role TEXT NOT NULL CHECK(role IN ('student', 'admin')))
# change to
CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username VARCHAR(256) UNIQUE NOT NULL, password VARCHAR(256) NOT NULL, role VARCHAR(256) NOT NULL CHECK(role IN ('student', 'admin')))

# Works fine in mysql
CREATE TABLE IF NOT EXISTS user_class_enrollment (
user_id INTEGER NOT NULL, class_id INTEGER NOT NULL,
PRIMARY KEY (user_id, class_id),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (class_id) REFERENCES classes (id) ON DELETE CASCADE
)

Now all you have to do is wade through all the other queries, trying them, see if they work, altering them if they don't according to the error you get. Personally, I don't feel like wading through 1126 lines of code, although, it is not really a difficult task.

Install phpmyadmin on your machine, log in, then you can try any mysql query you like, see if it works!

Ways to try to "automate the boring stuff": try loading your file as text and using .replace(). Maybe there are other similar "quick-fixes".Work on a copy though!

with open(mycode_copy) as infile:
    text = infile.read()
    text.replace("TEXT UNIQUE NOT NULL", "VARCHAR(256) UNIQUE NOT NULL")
Good Luck!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with MySql Connection EMG2024 2 1,699 May-29-2024, 03:11 PM
Last Post: EMG2024
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 2,320 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Virtual Env changing mysql connection string in python Fredesetes 0 1,462 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Mysql and mysql.connector error lostintime 2 2,561 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Too many queries? lorasf 6 3,126 Jul-04-2023, 04:27 AM
Last Post: lorasf
  Mysql error message: Lost connection to MySQL server during query tomtom 6 24,385 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Python MYSQL connection does not work after 1h idle zazas321 9 15,965 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  Serial connection connection issue Joni_Engr 15 16,482 Aug-30-2021, 04:46 PM
Last Post: deanhystad
  Text File Manipulation Queries? JustJeff 2 3,466 Apr-10-2021, 08:12 PM
Last Post: JustJeff
  Migrating to Mysql from SQlite atari400 10 7,760 Nov-23-2019, 09:56 PM
Last Post: atari400

Forum Jump:

User Panel Messages

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