skip to Main Content

I am trying to insert a txt file into mysql table but it is not working. I have tried using LOAD_FILE but it gave me an error code – MySQLdb.IntegrityError: (1048, "Column ‘transcript’ cannot be null")

cursor.execute("insert into `movies` (`movie_name`,`movie_file`,`movie_password`,`transcript`) values (%s,%s,%s, LOAD_FILE('/APPLES/transcription.txt'))",[moviename,moviefile,password])

I keep getting an Integrity Error
MySQLdb.IntegrityError: (1048, "Column ‘transcript’ cannot be null")
Could someone please help me in inserting a file into sql table. Thank you in advance

2

Answers


  1. The error you’re encountering with LOAD_FILE() in MySQL usually occurs when the file is not accessible by MySQL due to reasons like the file not being on the MySQL server, permissions issues, or restrictions set by MySQL’s secure-file-priv option. To address this, ensure the file exists on the MySQL server with the correct permissions and is within a directory allowed by secure-file-priv. Alternatively, you can bypass these limitations by reading the file content in your Python code and inserting it directly into the database:

    with open('/APPLES/transcription.txt', 'r') as file:
        transcript_content = file.read()
    
    print(transcript_content)
    
    cursor.execute("insert into `movies` (`movie_name`, `movie_file`, `movie_password`, `transcript`) values (%s, %s, %s, %s)", [moviename, moviefile, password, transcript_content])
    
    
    Login or Signup to reply.
  2. There could be a problem with file permissions, check that.

    otherwise, you can try to read it using python and then insert the data directly into SQL.

    with open('PATH TO YOUR FILE', 'r') as file:
        data = file.read()
    
    cursor.execute("insert into `movies` (`movie_name`, `movie_file`, `movie_password`, `transcript`) values (%s, %s, %s, %s)", [moviename, moviefile, password, data])```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search