0

nice to meet you all. I'm new to Python cgi and have a bit of experience with PHP. Using PHP I created a To do list program. After completing it I decided to write the same program with Python cgi to check how different it would be. But because I'm really new to Python I got stuck on basic knowledge on how to delete a row.

<?php

require_once 'init.php';

if(isset($_GET['item'])){

   $id = $_GET['item'];

   if(!empty($id)){
       $deleteQuery = $db->prepare("
           DELETE
           FROM items
           WHERE id=:id
       ");

       $deleteQuery->execute([
           'id' => $id
       ]);

   }

};

header('Location:webPHP.php');

With PHP it is simple to retrieve a specific part of a row but how can I do it and confirm if it is not empty with Python?

4
  • SQL, SQLite, something else? Commented Apr 12, 2017 at 15:57
  • mysql using wamp server Commented Apr 12, 2017 at 16:01
  • I don't have mysql running on this computer, therefore can't offer a direct answer. If you were to search SO for python cgi mysql you would get a collection of results, some of which might be useful. Indeed stackoverflow.com/questions/22443209/… might be enough for you. If the result of cursor.fetchall is empty (ie, None) then you know rows you've designated in your SQL select do not exist. Commented Apr 12, 2017 at 17:04
  • Yes I seen this. But how can I select a specific part of a row and check if it is empty or not? In example above I check if an id of the line is not empty, how can I do this in Python? Commented Apr 12, 2017 at 17:28

1 Answer 1

0

I don't remember the answer to this. It's possible that it's similar to what happens with sqlite in which case you might benefit from the following. In essence, some Python libraries will change a NULL that is coming in from SQL to a None which Python understands.

import sqlite3

import os
os.chdir('c:/scratch')

conn = sqlite3.connect('temp.sqlite')
conn.row_factory = sqlite3.Row
c = conn.cursor()

c.execute('''SELECT * FROM JustDate WHERE Date_1 = "1970-01-23"''')

for row in c.fetchall():
    for key in row.keys():
        if row[key]:
            print ('Field ', key, 'is not NULL (', row[key], ')')
        else:
            print ('Field ', key, 'is NULL')

Field  Date_1 is not NULL ( 1970-01-23 )
Field  Date_2 is NULL
Field  Date_3 is not NULL ( 2014-08-17 )

This is a two-record database table. I select one of the records and indicate how you can check whether the content of a field is None or not.

In short, if you write if <name> and <name> evaluates to a value other than None, zero, a non-empty string (or one of a few other things) then this expression will evaluates to True. Otherwise, false.

Sign up to request clarification or add additional context in comments.

3 Comments

what does key stands for?
row.keys() is a dictionary. key is just a formal variable that assumes a series of values from the set of keys in the dictionary. Each value it assumes is a 'key' in the dictionary. I could have used any identifier instead of keys.
But according to your example you select a specific part of the data. I need to access data that I send from other page print ("<a href='delete.py?{}' class='done-button'>Delete</a>".format(x[0]))

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.