Coder Social home page Coder Social logo

motion-sqlite3's Introduction

Introduction

This is a tiny wrapper around the SQLite C API that's written in RubyMotion. It is intentionally bare so as to limit the surface area of code that interacts with the raw C API (misuse of the C API can cause resource leaks and seg faults). Think of it more as a driver for SQLite than anything else.

Whenever possible, it uses Ruby idioms like blocks and exceptions.

Code Climate Build Status Gem Version

Is it any good?

Yes.

Sample code

DB in memory

db = SQLite3::Database.new(":memory:")
db.execute("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)")
db.execute("INSERT INTO test (name, age) VALUES (?, ?)", ["brad", 28])
db.execute("INSERT INTO test (name, age) VALUES (?, ?)", ["sparky", 24])

rows = []
db.execute("SELECT * FROM test") do |row|
  rows << row
end

rows.should == [
  { id: 1, name: "brad", age: 28 },
  { id: 2, name: "sparky", age: 24 }
]

DB in your resources folder

# File is stored in your /project/resources/ folder
db = SQLite3::Database.new(File.join(App.resources_path, "my_db.sqlite"))

rows = []
db.execute("SELECT * FROM test ORDER BY id") do |row|
	rows << row
end

*this code assumes you're using BubbleWrap for access to App.resources_path.

Usage

  • Pass a filename to the constructor, or ":memory:". The database is held open for the lifetime of the object.
  • Use execute to run SQL statements. All SQL statements are first prepared, and parameters can be passed as an Array or a Hash. If a Hash is passed, then the SQLite named parameter syntax is assumed to be in use.
  • Use execute_debug to see the SQL statement and paramaters passed in the REPL. You should not use this method in production.
  • Use execute_scalar to run SQL statements and return the first column of the first row. This is useful for queries like SELECT COUNT(*) FROM posts.

motion-sqlite3's People

Contributors

andrewhavens avatar ben5516 avatar markrickert avatar mattgreen avatar solanoluz avatar wndxlori avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

motion-sqlite3's Issues

motion-sqlite3 conflicts with ServiceManagement

Hey matt,

Love the gem!

There seem to be a conflict with Apple's ServiceManagement framework. After adding motion-sqlite3 gem to my project, the SMLoginItemSetEnabled function is no longer accessible:

app_delegate.rb:3:in `applicationDidFinishLaunching:': undefined method `SMLoginItemSetEnabled' for #<AppDelegate:0x7ffc7072fea0> (NoMethodError)

Any ideas why this could be? I'm about to release an awesome app that uses motion-sqlite3 ๐Ÿ‘

Cheers Jonas

Unable to open database file

Getting this error:

<Error>: *** Terminating app due to uncaught exception 'SQLite3::SQLite3Error', reason: 'unable to open database file (SQLite3::SQLite3Error)

This error only appears on the device and not on the simulator. All of the DB access is done via the main thread only from a single instance of the Database class.

Database path: File.join(App.resources_path, "offline_store.sqlite3")

Warning on compile: libsqlite3.a the table of contents is empty

ยฑ |remove-logging โœ—| โ†’ rake spec
     Build ./build/iPhoneSimulator-6.1-Development
     Build vendor/sqlite3
/Applications/Xcode.app/Contents/Developer/usr/bin/ranlib: file: libsqlite3.a(dummy.m.o) has no symbols
/Applications/Xcode.app/Contents/Developer/usr/bin/ranlib: warning for library: libsqlite3.a the table of contents is empty (no object file members in the library define global symbols)
   Compile /Users/mrickert/Dropbox/Rickert/motion-sqlite3/lib/motion-sqlite3/database.rb
      Link ./build/iPhoneSimulator-6.1-Development/motion-sqlite3_spec.app/motion-sqlite3
    Create ./build/iPhoneSimulator-6.1-Development/motion-sqlite3_spec.dSYM
  Simulate ./build/iPhoneSimulator-6.1-Development/motion-sqlite3_spec.app

BridgeSupport problem?

Hi,

I've just started with RubyMotion and your sqlite3 driver was working great. But after upgrading RubyMotion to 2.22 (from 2.18),

     Build ./build/iPhoneSimulator-7.0-Development
   Compile /Users/jviney/Work/rm/nzf-1/vendor/bundle/ruby/2.0.0/gems/motion-sqlite3-0.5.0/lib/motion-sqlite3/version.rb
   Compile /Users/jviney/Work/rm/nzf-1/vendor/bundle/ruby/2.0.0/gems/motion-sqlite3-0.5.0/lib/motion-sqlite3/result_set.rb
   Compile /Users/jviney/Work/rm/nzf-1/vendor/bundle/ruby/2.0.0/gems/motion-sqlite3-0.5.0/lib/motion-sqlite3/error.rb
   Compile /Users/jviney/Work/rm/nzf-1/vendor/bundle/ruby/2.0.0/gems/motion-sqlite3-0.5.0/lib/motion-sqlite3/statement.rb
2014-02-06 18:46:15.936 ruby[43808:d0f] can't open BridgeSupport full file at path `build/sqlite3.bridgesupport': Error Domain=NSXMLParserErrorDomain Code=4 "zero length data" UserInfo=0x62c1af0 {NSLocalizedDescription=zero length data}
2014-02-06 18:46:15.941 ruby[43807:d0f] can't open BridgeSupport full file at path `build/sqlite3.bridgesupport': Error Domain=NSXMLParserErrorDomain Code=4 "zero length data" UserInfo=0x5d0aed0 {NSLocalizedDescription=zero length data}
r

The bizarre thing is that if I create a new RubyMotion project and copy the application code in, it works fine.

Any idea what's causing this error?

P.S. Thanks for writing this library!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.