Thursday, April 28, 2011

Inserting records with SQL Lite

What is the SQL Lite statement for "insert"?

I have tried

insert into table1 values(field1,field2) values (?,?)

but this returns an error in the statement -- how do I do this?

From stackoverflow
  • The correct syntax for the insert statement is here:

    INSERT INTO table (column1, [column2, ... ]) VALUES (value1, [value2, ...])
    

    Note that you have put the VALUES keyword before the list of column names.

    Brad Larson : If it works, you may want to accept the answer.

Immutability and thread safety in Python

I'm cleaning some of the Python code I wrote when I was...not as knowledgeable. Primarily I am killing some of the complexity that stemmed from an incomplete understanding of threading in Python. I need to make a list of items thread-safe, and I'd like to do it via immutable lists, instead of the usual locking approach. I know that immutable objects are very special with regard to threading because all the thread-safety issues surrounding incomplete state changes simply disappear.

So, I ask: is the following code thread-safe?

class ImmutableList(object):
    def __init__(self):
        self._list = ()

    def __iter__(self):
        return self._list.__iter__()

    def append(self, x):
        self._list = self._list + tuple([x])

I think it is, because a new list is constructed each time. If the list is updated while another thread is iterating through it, the old list will continue to be used for the remainder of the iteration. This is fine by me, but may not be for everyone.

Also, is this a good idea? I only want to apply this to a few situations where the list size is small, and the lists aren't changed much (event listeners spring to mind).

From stackoverflow
  • First of all, appending to a list is already thread-safe in the CPython reference implementation of the Python programming language. In other words, while the language specification doesn't require that the list class be thread-safe, it is anyway. So unless you're using Jython or IronPython or some other Python implementation like that, then you're fine.

    Second, you'd also need to overload the other list operations, such as __setitem__ and __setslice__, etc. I'm assuming that your implementation handles this.

    Finally, the answer to your question is no: your code isn't thread safe. Consider the following situation:

    • Your list contains (5, 6)
    • Thread 1 tries to append 7, and Thread 2 tries to append 8
    • Thread 1 constructs another tuple (5, 6, 7) and before that can be assigned to _list, there's a context switch
    • Thread 2 performs its assignment, so the list is now (5, 6, 8)
    • Thread 1 gets control of the CPU back and assigns to _list, overwriting the previous append. The list is now (5, 6, 7) and the 8 has been lost.

    The moral of this story is that you should use locking and avoid cleverness.

    Matt Green : Never thought of this case...thanks!
    S.Lott : +1: Just use locks.
  • A true immutable list implementation will not allow the underlying list structure to change, like you are here. As @[Eli Courtwright] pointed out, your implementation is not thread safe. That is because it is not really immutable. To make an immutable implementation, any methods that would have changed the list, would instead return a new list reflecting the desired change.

    With respect to your code example, this would require you to do something like this:

    class ImmutableList(object):
      def __init__(self):
        self._list = ()
    
      def __iter__(self):
        return self._list.__iter__()
    
      def append(self, x):
        return self._list + tuple([x])
    
    Eli Courtwright : +1 for your example of how immutable data structures really work. However, I should point out that your example would be unhelpful to Matt Green, who needs a data structure that can be concurrently modified by multiple threads.
    1800 INFORMATION : I don't think that is what he really needs, actually it is difficult to tell what it is he needs from the description, but since he is talking about immutable structures it seems like he either doesn't want to concurrently modify it, or he doesn't really understand the correct usage for immutable

streaming avi file issue

Hello everyone,

I am writing a video streaming server application. I have an avi file and I put it on IIS 7 for streaming. And find I cannot jump to an arbitrary location of the media if the media is not buffered already. I think some meta-data is missing during recording? After some search work, I think it may be caused by the index block of avi file is stored at the end, so Windows Media Player could not jump to any arbitrary location before buffered locally.

Here is an example, if my recorded avi video is 10 mins, and now I am playing to the 4th mins, and the local buffered streaming media is buffered to the 5th mins, I cannot jump to any time after the 5th mins, like the 7th mins in Windows Media Player.

BTW: other formats like asf/wmv did not have the same issue on the same server, so I think it should be an issue related to avi file, not server or environmental issues.

My questions are,

  1. What is the actual cause of this issue -- can not jump to arbitrary location before buffered for avi file?
  2. Any solutions or walkarounds? Like some smarter players instead or add some code fix at server side?

thanks in advance, George

From stackoverflow
  • As you already discovered yourself, index is written at the end of avi containers. That's why avi format is not good for streaming.

    You should convert your video to some other format which has the indexing information at the beginning. For example, flv, wmv, ogg/theora, etc.

    George2 : Hi Milan, conversion is too slow... Do you have any ideas to speed up? I have a video of 30mins, and it takes 20 mins to convert to flv. My computer is powerful, 4G RAM and 2 CPUs. Input avi file size is about 150M Bytes.
    George2 : @Milan, sorry another question. If you think converting to other format like flv/wmv is the best solution, could you recommend some video conversion solutions, like H/W? I think using CPU to convert video is really slow... :-)
    Milan Babuškov : Well, if you want to keep the quality, it has to take the time. It would be best if you had some non-lossy source before it was in avi format. For encoding/recoding I use mencoder and ffmpeg programs, but there are many of them out there. Maybe you should post a separate question about this.
    George2 : Hi Milan, 1. "Well, if you want to keep the quality, it has to take the time." -- I do not need quality, I just need to speed-up encoding process as fast as possible. Do you have any advice?
    George2 : 2. " It would be best if you had some non-lossy source before it was in avi format" -- I am using Camtasia to record screen for a Demo, I think in this case there is no non-lossy source, correct?
    Milan Babuškov : Maybe you could convert the video from AVI to ASF format using a simple copy (i.e. the content is the same, but container changes). Try this program for example: http://www.videohelp.com/tools/MediaCoder
    George2 : Great idea Milan! I have downloaded and installed MediaCoder, but (I am a new user) I did not find an option for me to copy from avi to asf. Could you show me more instructions?
    Milan Babuškov : Perhaps you would get more answers asking at a forum specialized for this. SO is only about programming, after all. Try here: http://www.videohelp.com
    George2 : Thanks Milan, I have found this videohelp forum quite useful.
    George2 : @Milan, I have started a new topic as you suggested above to discuss further new topic, if you have expertise, please help here, http://stackoverflow.com/questions/743429/codes-to-convert-from-avi-to-asf

add a UIView above tableView problem

i have add a subView to UITableView in my application,after adding, the tableViewCell still clickable,how to prevent this?pls help

From stackoverflow
  • If I understood your problem this time, if you can make the subview the size of the screen, then you just need to set userInteractionEnabled to TRUE on the subview.

    Tested it with a simple UITableView and a UIImageView over it. If I disable the userInteractionEnabled then the willSelectRowAtIndexPath of the tableView delegate is called, if I set it to TRUE nothing happens.

    issac : i donot want my tableView not clickable at all time, jus when i add the subview above the tableView and it just unclickable. the problem i have is when i add a new view , the tableView was behind but still can click and link to corresponding view the viewcontroller i used is tableViewController

Where can I buy the QTP tool?

I am downloading the trial version of QTP but I cannot find the link to buy the QTP tool.

Where can I buy QTP?

From stackoverflow
  • You need to just contact the vendor directly. I doubt you will be able to just buy it on the Web.

    https://h10078.www1.hp.com/cda/hpms/display/main/hpms_content.jsp?zn=bto&cp=1-11-127-24^1352_4000_100__

    Look at the "Contact HP" spot on the top right.

    Simon Lieschke : Don't make it as hard as HP to buy the software, make your link clickable! :p
    BobbyShaftoe : I tried, I think Markdown has problems with characters in the URL.
    Xiaofu : Hopefully sivaramakrishna has contacted HP by now. If not, follow Bobby's link. If you want to email them for Sales enquiries you'll need an HP Passport by the looks of it (just to make it extra hard), or you can call the sales office at: India Phone: (+91) 80 2504-1256
    yoosiba : Character '^' makes problem. You need to encode it properly. I had the same problem when posting other link to HP. issue is described here: http://meta.stackoverflow.com/questions/30379/link-recognition-broken-in-question-and-in-comment-it-works-incorrectly

How to highlight the selection in emacs keybinding of eclipse?

I have searched the Web for this, but with no luck. Any idea?

Thanks.

From stackoverflow
  • As described here, you can set emacs key binding to work in component editor in Eclipse

    Go to Preferences -> General -> Keys.

    • Change Scheme to Emacs if you haven't done so already, so you can easily spot the emacs key bindings.
    • Sort by Category and look at the "Text Editing" categories
    • Find the commands that you care about, e.g. Next Column, Previous Column
    • Select that command and click "Copy Command"
    • Set the binding to the key by pressing the keys, e.g. press Control-f to generate "^F".
    • Change "When:" to "Editing Components".
    • Repeat for all the key bindings you care about.

    So if, in emacs, selection is done by setting the mark (Control-Space) at the start of the region, then moving the point to to the end of the region, you could associate the appropriate shortcut to:

    Text Editing > Select Text Start
    Text Editing > Select Text End
    
    Angus : After binding "Control-Space" to the command "Select Text Start", I found that the eclipse editor always highlight the region which ranges from the first line of current file to the mark. Any suggestion? Thanks again.
    VonC : I will look again at the various select command, but if none fit the expected behavior, it will be tricky: a command and its associated key would have to be defined in order to properly select the text.
  • Try using the Emacs+ plugin available here:

    http://www.mulgasoft.com/emacsplus

    It provides a HUGE range of functionality - including the selection highlighting that you're looking for - that really helps the move from Emacs. I've only been using it for a couple of days, but it appears stable and integrates very well with the editor.

Can a breakpoint be set for EXC_BAD_ACCESS?

Just as you can set a breakpoint for objc_exception_throw, is there an equivalent for EXC_BAD_ACCESS?

From stackoverflow
  • EXC_ BAD_ ACCESS is an exception (EXCeption_ BAD_ ACCESS). If you set a breakpoint on objc_exception_throw, you'll get those. You might want to look at NSZombieEnabled, as you're probably trying to access a dealloc'd object.

    Jesse Rusak : EXC_BAD_ACCESS can be caused in many ways that have nothing to do with objective-c exceptions.
  • cheers Ben it works :)