更新GAE数据存储区中的条目

时间:2015-07-07 17:00:29

标签: python google-app-engine flask

快速提问(可能是基本问题!)。

我正在使用带有Flask的Google App Engine。我有以下数据库:

class Bear(db.Model):
bear_id = db.IntegerProperty()
pair = db.IntegerProperty()
status = db.IntegerProperty()
time = db.IntegerProperty() 

我在此处通过网址参数添加:

@app.route('/add')
def add():
    rightNow = int(time.time())
    cur_id = int( request.args.get('id') )
    cur_status = int( request.args.get('status') )

    newEntry = Bear(
        time = rightNow,
        bear_id = cur_id,
        status = cur_status,
        # key_name = str(cur_id)
    )

    newEntry.put()  

    return 'added'

并且(尝试)再次通过URL参数更新它:

@app.route('/update')
def update():
    rightNow = int(time.time())
    cur_id = int( request.args.get('id') )
    cur_status = int( request.args.get('status') )

    address_k = db.Key.from_path('bear_id', cur_id)

    address = db.get(address_k)

    address.status = cur_status;
    address.put()   

    return 'cur_id'

我试图通过使用bear_id字符串来拉取数据存储区实体,但我注意到我收到了存储在address_k中的错误密钥。它与我在数据存储区中看到的密钥不匹配。我不确定为什么会这样。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

发生这种情况是因为您使用id作为int进行插入,并使用id作为str进行检索。数据存储区以不同方式对待它们,因此您应保持一致。

答案 1 :(得分:0)

我最终做了以下事情:

另外,我将key_name添加为ID

的字符串
private void Playlist_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;  
}

private void Playlist_DragDrop(object sender, DragEventArgs e)
{
    //get the file names
    string[] songs = (string[])e.Data.GetData(DataFormats.FileDrop, false);

    //we're using a Parallel.ForEach loop because if a directory is selected it can contain n number of items, this is to help prevent a bottleneck.
    Parallel.ForEach(songs, song =>
    {
        //make sure the file exists
        if (File.Exists(song))
        {
            //if it's an mp3 file then call AddFileToListview
            if (string.Compare(Path.GetExtension(song), ".mp3", true) == 0)
            {
                AddFileToListview(song);
            }
        }
        //A HA! It's a directory not a single file
        else if (Directory.Exists(song))
        {
            //get the directory information
            DirectoryInfo di = new DirectoryInfo(song);

            //get all the mp3 files (will add WMA in the future)
            FileInfo[] files = di.GetFiles("*.mp3");

            //here we use a parallel loop to loop through every mp3 in the
            //directory provided
            Parallel.ForEach(files, file =>
            {
                AddFileToListview(file.FullName);
            });
        }
    });
}

private void AddFileToListview(string fullFilePath)
{
    double nanoseconds;
    string totalTime = string.Empty;

    //First things first, does the file even exist, if not then exit
    if (!File.Exists(fullFilePath))
        return;

    //get the song name
    string song = Path.GetFileName(fullFilePath);

    //get the directory
    string directory = Path.GetDirectoryName(fullFilePath);

    //hack off the trailing \
    if (directory.EndsWith(Convert.ToString(Path.DirectorySeparatorChar)))
        directory = directory.Substring(0, directory.Length - 1); 

    //now we use the WindowsAPICodePack.Shell to start calculating the songs time
    ShellFile shell = ShellFile.FromFilePath(fullFilePath);

    //get the length is nanoseconds
    double.TryParse(shell.Properties.System.Media.Duration.Value.ToString(), out nanoseconds);

    //first make sure we have a value greater than zero
    if (nanoseconds > 0)
    {
        // double milliseconds = nanoseconds * 0.000001;
        TimeSpan time = TimeSpan.FromSeconds(Utilities.ConvertToMilliseconds(nanoseconds) / 1000);
        totalTime = time.ToString(@"m\:ss");
    }

    //build oour song data
    ListViewItem item = new ListViewItem();
    item.Text = song;
    item.SubItems.Add(totalTime);

    //now my first run at this gave me a cross-thread exception when trying to add multiple single mp3's
    //but I could add all the whole directories I wanted, o that is why we are now using BeginINvoke to access the ListView
    if (Playlist.InvokeRequired)
        Playlist.BeginInvoke(new MethodInvoker(() => Playlist.Items.Add(item)));
    else
        Playlist.Items.Add(item);            
}

然后在更新时,我能够像这样拉动它:

@app.route('/add')
def add():
    rightNow = int(time.time())
    cur_id = int( request.args.get('id') )
    cur_status = int( request.args.get('status') )

    newEntry = Bear(
        time = rightNow,
        bear_id = cur_id,
        status = cur_status,
        key_name = str(cur_id)
    )

    newEntry.put()  

    return 'added'

它有效!