Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a record using GQL?

Tags:

python

gql

I need to iterate and delete all records of my datastore. I am using Google App engine Launcher to test it on local host. How to do it?

When I am trying to delete all recors in Person model that way:

 qObj = Person.all()
 db.delete(qObj)

I am getting error BadValueError: Property y must be a str or unicode instance, not a long I guess there is conflict in Model data types.

class Person(db.Model):
    name = db.StringProperty()
    x = db.StringProperty()
    y = db.StringProperty()
    group = db.StringProperty()

The field y = db.StringProperty() previously was y = db.IntegerProperty(). At this moment I need to flush all db records. How can I do that?

Is there is an opportunity to delete local file which stores all db records?

like image 834
J.Olufsen Avatar asked Jan 05 '12 03:01

J.Olufsen


People also ask

How do I delete a record in GraphQL?

Using the parse GraphQL, there are two different ways to delete an existing object in your database: Using generic mutation - this is the mutation that you can use to delete an object of any class. Using class mutation - this is the recommended mutation that you should use to delete an object of a specific class.

Which command is used to delete the records?

The DELETE command is used to delete specified rows(one or more). While this command is used to delete all the rows from a table. It is a DML(Data Manipulation Language) command. While it is a DDL(Data Definition Language) command.

How do you delete a record in Python?

Practical Data Science using Python To delete records from a MySQL table, you need to use the DELETE FROM statement. To remove specific records, you need to use WHERE clause along with it.


2 Answers

The GQL language can only be used to retrieve entities or key (cf. http://code.google.com/appengine/docs/python/datastore/gqlreference.html)

You'll have to do this:

persons = Person.all()

for p in persons:
    p.delete()

Regarding the error BadValueError: Property y must be a str or unicode instance, not a long, you'll have to modify all the data (from integer to string) in the database to resolve the conflict.

It seems that you want to delete everything, so another solution would be to just go to the datastore administration page - http://localhost:8080/_ah/admin on localhost or via https://appengine.google.com/ - and remove everything.

You might find this useful: http://code.google.com/appengine/articles/update_schema.html

like image 136
charlax Avatar answered Sep 23 '22 03:09

charlax


If you have a variable that stores a record on the database then you can simply use delete().

That is, say you have an Entity called Persons, you can do:

personToDelete = db.GqlQuery("SELECT * FROM Persons WHERE name='Joe'");
person = personToDelete[0];
person.delete();

You do also have to import the database library, but I'm assuming you do that anyway given that you're clearly using the database.

like image 21
pinerd314159 Avatar answered Sep 22 '22 03:09

pinerd314159