0

This is the error I get:

Traceback (most recent call last):

File "/lib/python3.10/site-packages/_pyodide/_base.py", line 435, in eval_code
.run(globals, locals)

File "/lib/python3.10/site-packages/_pyodide/_base.py", line 304, in run
coroutine = eval(self.code, globals, locals)

File "", line 3, in
File "/home/pyodide/connection_sqlite3.py", line 45, in returnGeral
data = cursor.execute(r"SELECT * FROM tabela;")

AttributeError: 'NoneType' object has no attribute 'execute'

A lot of the code in connection_sqlite3 needs work, it's just I don't understand where is the error comes from.

I try the Python code alone with a database, and it works, but when connected with html then this error appeared.

HTML markup:

type <!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8"/>
  <title>index</title>
  <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
  <link rel="stylesheet" type="text/css" href="style/style.css">
  <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>
  <body>
    <py-config>
        [[fetch]]
        from = 'scripts/'
        files = ['connection_sqlite3.py'] 
    </py-config>
    <py-script src="scripts/main.py"></py-script>
    <div id="principal">
        <div id="navegation">
            <input placeholder="teste" type="text">
            <button py> search </button> 
        </div>
        <div id="table">

        </div>
    </div>
  </body>
</html> here

main.py file

import connection_sqlite3 as sql

sql.returnGeral()

connection_sqlite3.py file

import sqlite3
from sqlite3 import Error

data = 'data/tutorial.db'

#query para criacao de tabelas
def templates(arquivo):
    raw = open(arquivo)
    edited = raw.read()
    return edited

#funcoes gerais

def connection():
    try:
        conn = sqlite3.connect(data)
        cursor = conn.cursor()
        return conn
    except Error as e:
        print(f"{e}, and failed in the line 14")
    

def createTable(archive):
    try:
        conn = connection()
        cursor = conn.cursor()
        cursor.execute(archive)
    except Error as e:
        print(f"{e}, error in the line 23")

def insertData(param):
    try:
        cursor = connection()
        cursor.execute(param)
        conn = connection()
        conn.commit()
        print(fr"{param} was inserted in the table" )
    except Error as e:
        print(f"{e}, failed at line 41")

def returnGeral():
    try:
        connection()
        cursor = connection()
        data = cursor.execute(r"SELECT * FROM tabela;")
        raw = data.fetchall()
        for i in raw:
            print(i)
        return data
    except Error as e:
        print(f"{e}, error in the line 34")
      
def execute(param):
    try:
        conn = connection(1)
        cursor = connection(2)
        cursor.execute(param)
        conn.commit()
        print(f'{param}, was executed successfully')
        
    except Error as e:
        print(f"{e} error in line 64")

I was trying to create a database and link it with pyscrit to insert and return data in a web server, but for now it's just the local part.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Recknover
  • 1
  • 1
  • 1
    Don't post errors as images. Post all code, input/output, and errors as plain text. – John Gordon Jan 19 '23 at 02:17
  • ok, sorry, i ajusted the error, thank you – Recknover Jan 19 '23 at 02:40
  • This line of code is failing `cursor = conn.cursor()`. Does the directory `data` exist in the browser's virtual file system? Remember that the virtual file system is recreated each time the page loads. I do not see your code creating that directory. Pyodide supports persistent mountable file systems. For a database, you probably want to use that. – John Hanley Jan 19 '23 at 20:03
  • this seens to be the problem, i transfer the database to the same folder as the html file, and it works, but one question, how do i add a folder to the virtual file system, you don't need to answer me if you don't want, you already help enough thank you. – Recknover Jan 21 '23 at 00:55
  • The browser virtual file system is supported by the Python API. That is the file system used for Python file APIs unless you mount another file system to the virtual file system. Use `os.mkdir()`. https://www.geeksforgeeks.org/create-a-directory-in-python/ – John Hanley Jan 21 '23 at 01:00

0 Answers0