Jump to content

I have a main.py file and in that is

import sqlite3from functions import *db = sqlite3.connect('accounts.db')c = db.cursor()

and a functions.py file and in that is

def check_db():    c.execute("""CREATE TABLE IF NOT EXISTS accounts (id NOT NULL AUTOINCREMENT PRIMARY KEY, username VCHAR(255) NOT NULL, password VCHAR(255) NOT NULL ) """)    db.commit()

my problem is when I run check_db() I get "global name 'c' is not defined", how can i fix this?

 

thanks

Link to comment
https://linustechtips.com/topic/73883-python3-global-name-is-not-defined/
Share on other sites

Link to post
Share on other sites

Did you import the module that .cursor() is in? It seems like the c in the first bit of code may be what's throwing you. I'm not 100% sure though. I've never used this before.

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to post
Share on other sites

I'm still very much a beginner when it comes to Python, but don't you need to

import main.py into functions.py for this to work?

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to post
Share on other sites

Listen to what @MikeD says :D

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to post
Share on other sites

import sqlite3from functions import *db = sqlite3.connect('accounts.db')c = db.cursor()check_db(c, db)
def check_db(c, db):    c.execute("""CREATE TABLE IF NOT EXISTS accounts (id NOT NULL PRIMARY KEY, username VCHAR(255) NOT NULL, password VCHAR(255) NOT NULL ) """)    db.commit()

You actually need both c and db.

Passing arguments to functions is just a matter of declaring that the function receives something and calling the function with the actual object you have.

Link to post
Share on other sites

thanks alot

 

 You're welcome!

 

 

Listen to what @MikeD says :D

 

But make sure to double check because it might be all wrong!

[spoiler "Funny story actually..."]Today I had a Natural Language test with a few multiple choice questions and at the very last second I decided to change my mind about the answer to a couple of them and I ended up changing from right to wrong... yeahhh.... :( 

Link to post
Share on other sites

But make sure to double check because it might be all wrong!

[spoiler "Funny story actually..."]Today I had a Natural Language test with a few multiple choice questions and at the very last second I decided to change my mind about the answer to a couple of them and I ended up changing from right to wrong... yeahhh.... :( 

Don't let it get to ya bud, what happens in the real world is what really mattrers.

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×