Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'importing': 0.04; 'cursor': 0.09; 'func': 0.09; 'nameerror:': 0.09; 'def': 0.10; 'value.': 0.15; 'connect()': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instead:': 0.16; 'scope.': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'received:209.85.214.174': 0.21; '"",': 0.22; 'object.': 0.22; 'defined': 0.22; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'message- id:@mail.gmail.com': 0.27; '>>>>': 0.29; "skip:' 10": 0.30; 'returned': 0.30; 'error': 0.30; 'code': 0.31; 'file': 0.32; 'point,': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'nov': 0.35; 'pm,': 0.35; 'posting': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; '12,': 0.36; 'thank': 0.36; 'execute': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'your': 0.60; 'easy': 0.60; 'side': 0.61; 'gave': 0.65; 'subject:there': 0.65; 'below:': 0.71; '9:45': 0.84; 'clearly.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=cy7g5UFF/NyCrZzl/zyvFjjJO8Pe+SxWCnCDr2aaEH8=; b=eNf4iypFkzZMOAAY5Q2P8h+bNVyXQoXK+nQNgvN6ER2LG8iFAUPQ+KMSjjWT8eI0Mn j6uAZ1el4PiOPrt51cUszoYQurR9n+zw4O40zmz23OIqonQyq+NZwi94XyObMFdmftqt m1Tmt22r8rrM9DKpd6W/UBFifMnVAW8i5D05HzIUAaupstTgOfiKOYy7gXnB7IwOuEvQ JmTE8GKv+iw+zDMh7EZk4uJKFCMFgik/bcs+HU+HoPJlCaaGh2tRoHqWTMfIhJtJ8th5 gO2dNMbPQ4/X17LpBmeYiSkB5I7KEM5BstxlcnP6El0NbYCNVEbcdiwS6AikYzPs7QyG DfeQ== MIME-Version: 1.0 In-Reply-To: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> References: <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> Date: Mon, 12 Nov 2012 22:01:13 +1100 Subject: Re: Is there a way to creat a func that returns a cursor that can be used? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352718075 news.xs4all.nl 6890 [2001:888:2000:d::a6]:59019 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33175 On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi wrote: > Is there a way to create a func that returns a cursor that can be used to execute sql statements? Yes, and you're almost there! > I tried this (after importing sqlite3), but it gave me the error below: > >>>> def connect(): > return cur >>>> connect() > >>>> cur.execute("select * from schedule") > Traceback (most recent call last): > File "", line 1, in > cur.execute("select * from schedule") > NameError: name 'cur' is not defined All you need to do is make use of the return value. Try this instead: cur = connect() That takes the returned cursor object and binds it to the name 'cur' in global scope. You can then use 'cur.execute...' and it'll be the same object. As a side point, thank you for posting so clearly. It's easy to help when your code and traceback are all there! ChrisA