optimize and fix bug on sqlite operations