[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Re-sorting Sqlite table

Posted on 2010-09-22
5
Medium Priority
?
678 Views
Last Modified: 2012-05-10
I would like to know if there is an SQL command that would re-sort the a table in a SQLite database. I have a table with 4 columns, which gets data added and removed from it frequently.
Just from a neatness point of view (as the messiness of the table does not have any negative impact on SQL queries on the table data), I'd like to be able to sort the table.
I've attached a screen grab of the table where you can see 4 records with Dept_Id value 1 and 4 records with Dept_id value 2. I'd like to sort the table so I get all the Value 1s followed by the Value 2s etc. Screen grab of SQlite table I'd like to sort.
0
Comment
Question by:kalbarriman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:jon47
ID: 33732920
There isn't an SQL command which reorders the records stored in a table.  What you can do is pull the data out sorted in whatever way you choose- so rather than browsing the table, use the "Execute SQL" tab in your sqlite browser and use a query like this:

SELECT * FROM DeptExts ORDER BY `Dept Id`, `Extn Name`

(Those a back-ticks around the column names)

Jon

0
 

Author Comment

by:kalbarriman
ID: 33732958
Thanks Jon. Yes, no problem pulling the data from the table in any sort order I want, but sorry to hear, (although not surprised as I've been checking on the NET and reading books and can't find anything) that I can't actually re-sort the table itself.
I'll leave the question open for a bit longer just in case someone else can magic up a solution but otherwise, thanks for your input.
Andrew
0
 
LVL 2

Accepted Solution

by:
jon47 earned 750 total points
ID: 33733396
Actually, here's some SQL that does what you want... it takes four statements-

CREATE TABLE DeptExtsSorted (Dept_Name TEXT, Extn_Name TEXT, Dept_Id NUMERIC, Dept_Ext NUMERIC);
INSERT INTO DeptExtsSorted (Dept_Name, Extn_Name, Dept_Id, Dept_Ext) SELECT Dept_Name, Extn_Name, Dept_Id, Dept_Ext FROM DeptExts ORDER BY Dept_Id, Extn_Name;
DROP TABLE DeptExts;
ALTER TABLE DeptExtsSorted RENAME TO DeptExts;

- you'll need to  check that the DeptExtsSorted table is created to match DeptExts, and check I've got the column names right too.
This creates a new table, sorts the data and inserts it into that new table, deletes the original, renames the sorted table to replace the original.

It's a bit warped, but appears to work ;-)

0
 

Author Comment

by:kalbarriman
ID: 33733425
Thanks Jon. Yes, I was thinking along similar lines (except I was going to suck out the data into an array, and then delete all the entries, and then re-insert them in sorted order), but I think your solution is better.
Apppreciate it. I'll close the Question now.
Andrew
0
 

Author Closing Comment

by:kalbarriman
ID: 33733430
There doesn't appear to be any way to do what I want with a single SQL command, but the solution resolves the issue in as good a way as possible.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question