1

I working on a project and I want to use one database in two python file but, when I run every project they created database for self if you know please tell me how I can use that

import sqlite3


def connect():
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute(
       "CREATE TABLE IF NOT EXISTS salary (id INTEGER PRIMARY KEY , name text, age INTEGER , price INTEGER )"
    )
    conn.commit()
    conn.close()


def insert(name, age, price):
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute(
        "INSERT INTO salary VALUES (NULL ,?,?,?)", (name ,age ,price)
    )
    conn.commit()
    conn.close()

def view():
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute(
        "SELECT * FROM salary"
    )
    rows = cur.fetchall()
    conn.close()
    return rows

def search(name="", age="", price=""):
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute(
        "SELECT * FROM salary WHERE name = ? OR age = ? OR price = ?", (name, age, price)
    )
    rows = cur.fetchall()
    conn.close()
    return rows

def delete(id):
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute("DELETE FROM salary WHERE id=?", (id,))
    conn.commit()
    conn.close()

def update(id, name, age, price):
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute(
        "UPDATE salary SET name = ?, age = ?, price = ? WHERE id = ?", (name, age, price, id)
    )
    conn.commit()
    conn.close()

def update_pay_money(name, price):
    conn = sqlite3.connect("waiters.db")
    cur = conn.cursor()
    cur.execute(
        "UPDATE salary SET price = ?  WHERE name = ?", (price, name)
    )
    conn.commit()
    conn.close()


connect()

enter image description here

1
  • 1
    Try something like conn = sqlite3.connect("../waiters.db") Commented Jul 10, 2021 at 14:19

2 Answers 2

2

Giving exact path like /path/to/waiters.db while connecting to your database should solve your problem?

This line should be changed while connecting to database.

conn = sqlite3.connect("/path/to/waiters.db")
Sign up to request clarification or add additional context in comments.

1 Comment

@mobinbagheri you haven't marked this answer as accepted answer, hasn't this helped you to solve your issue.
0

as Other mentioned for using a sqllite3 db in multiple files you can use their absolute or relative path, for example if you have 'DBs' & 'section-1' & 'section-2' directories and your python file are in section directories you can access the database file in each section by using somthing like this '"../DBs/waiters.db"' and so on for others... but whatf of you try make multiple tables in a database file in tgat way you don need to have multiple databases and its the standard way, hope it's help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.