Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:06:51+00:00 2026-06-16T09:06:51+00:00

I have a database built in Ruby using SQLite in the following way: db.execute

  • 0

I have a database built in Ruby using SQLite in the following way:

db.execute "CREATE TABLE IF NOT EXISTS Problems(ID INTEGER, stem BLOB NOT NULL, answer BLOB, datetime TEXT, lastmodifiedby TEXT, primary key (ID, datetime) )"
db.execute "INSERT INTO Problems VALUES(1, 'stem', 'answer', '12/26/2012 2:52:18 PM', 'bob')"
db.execute "INSERT INTO Problems VALUES(1, 'stem modified', 'answer', '12/26/2012 2:52:19 PM', 'bob')"
db.execute "INSERT INTO Problems VALUES(1, 'stem modified further', 'answer', '12/26/2012 2:52:20 PM', 'bob')"

The IDs for the first three entries are the same, however the times are different. I am currently using the following code to extract a single entry:

db = SQLite3::Database.new "#{dbname}"
stm = db.prepare "SELECT * FROM Problems WHERE ID=?"
stm.bind_param 1, id
rs = stm.execute  
problem = rs.next    

My first question – is there a way to condense the last 4 lines of code?

Second, when I select an entry from the Problems database, how would I add an option so that the most recent entry (in this case, the third one) is chosen?

And finally, how do I go about selecting all entries of a certain ID (here I only have the int 1, but in reality there are many others) so that I can output them as a string / write to a file, etc.

I have found answers to questions regarding most recent entry selection, but they seem quite complex. Would an ORDER BY work in some way?

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:06:52+00:00Added an answer on June 16, 2026 at 9:06 am

    First of all, I think you have a data format problem. I don’t think SQLite will understand '12/26/2012 2:52:18 PM' as a timestamp so you’ll end up comparing your timestamps as strings. For example, if I add '12/26/2012 2:52:20 AM' to the mix, I get '12/26/2012 2:52:18 PM' and '12/26/2012 2:52:20 PM' as the lowest and highest values and that only makes sense if they’re being compared as strings. Switch your data to ISO 8601 format so that you have these:

    2012-12-26 14:52:18
    2012-12-26 14:52:19
    2012-12-26 14:52:20
    

    and things will sort properly.

    Once you have that fixed, you can use ORDER BY and LIMIT to peel off just one record:

    stm     = db.prepare('select * from Problems where ID = ? order by datetime desc limit 1')
    rs      = stm.execute(1)
    problem = rs.next    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a local DVD Database using Codeigniter, With film names etc in.
I am using Ruby 1.8.7 and Rails 2.3.8 and I have the following root
I have a database that I built in SQLite browser, and it works fine.
I have a social network built on a relational database using Rails 3.0.7 and
Using the built-in Ruby Minitest framework, is there a way to run some code
I have an SQLite database with a table help with columns i_id , issue
I have built a DataTable from my database. Then I am looping through the
Suppose you have a messaging system built in PHP with a MySQL database backend,
I have for some time build n-tier Applications using a database server as the
I have created SQL Server 2008 Database project using visual studio 2010. after build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.