Copy MS SQL Server table to SQLite table











up vote
2
down vote

favorite












A little background: I am making an Android application that will remain offline most of the time. The application will need to sync with a MS SQL server database when it connects to wifi. The on-board database is SQLite. I am also very new to Java/Android development so I apologize if I'm missing the obvious somewhere.



I am able to make transactions with MS SQL server quite well using the JTDS driver however when I am querying the MS SQL server, I end up with a ResultSet object that I have to loop through one row at a time. Is there a tool/method out there than can simply insert all rows from a ResultSet into a SQLite database table?



I am currently using the standard:



ArrayList<ArrayList<String>> table = new ArrayList<>(); // a list of lists to represent a table
ArrayList<String> row = new ArrayList<>(); // a list to represent a row of the table

Class.forName("net.sourceforge.jtds.jdbc.Driver");

conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData meta = rs.getMetaData();

while (rs.next())
{
row = new ArrayList<>();

for (int i = 1; i < meta.getColumnCount() + 1; i++) // load data into List
{
row.add(rs.getString(i));
}

table.add(row);
}


From here I was going to loop through my list and add each row one by one to the SQLite database table. This seems inefficient to me. Was just looking for a better way to copy table from MS SQL Server to SQLite on an Android device.










share|improve this question
























  • Looks like a SELECT INTO could do the trick, quick and easy. w3schools.com/sql/sql_select_into.asp
    – Kling Klang
    Nov 9 at 17:10








  • 1




    @KlingKlang Because 'newtable' and 'oldtable' are from 2 totally different types of databases on two separate devices. I can't run a SQLite query from the Android app and query a MS SQL database with that query. I have to first get the data from the MS SQL Server, which will leave me with a JBDC/JTDS ResultSet object, as described in my post.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 17:27






  • 1




    @KlingKlang I could convert the ResultSet to a CSV somehow I'm sure but wouldn't that just be extra overhead? Like I said, I can parse the ResultSet into a SQLite table but it seems inefficient. I think it would be just as inefficient to go from ResultSet to CSV to SQLite table.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:13






  • 1




    @KlingKlang Though I do see a Linked Server option using an ODBC driver where I might be able to query MS SQL Server from SQLite: link and link. Maybe I'll give that a whirl.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:22






  • 1




    Or the other way around. (Throwing an INSERT INTO command from SQL Server to SQLite)
    – Kling Klang
    Nov 9 at 18:47















up vote
2
down vote

favorite












A little background: I am making an Android application that will remain offline most of the time. The application will need to sync with a MS SQL server database when it connects to wifi. The on-board database is SQLite. I am also very new to Java/Android development so I apologize if I'm missing the obvious somewhere.



I am able to make transactions with MS SQL server quite well using the JTDS driver however when I am querying the MS SQL server, I end up with a ResultSet object that I have to loop through one row at a time. Is there a tool/method out there than can simply insert all rows from a ResultSet into a SQLite database table?



I am currently using the standard:



ArrayList<ArrayList<String>> table = new ArrayList<>(); // a list of lists to represent a table
ArrayList<String> row = new ArrayList<>(); // a list to represent a row of the table

Class.forName("net.sourceforge.jtds.jdbc.Driver");

conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData meta = rs.getMetaData();

while (rs.next())
{
row = new ArrayList<>();

for (int i = 1; i < meta.getColumnCount() + 1; i++) // load data into List
{
row.add(rs.getString(i));
}

table.add(row);
}


From here I was going to loop through my list and add each row one by one to the SQLite database table. This seems inefficient to me. Was just looking for a better way to copy table from MS SQL Server to SQLite on an Android device.










share|improve this question
























  • Looks like a SELECT INTO could do the trick, quick and easy. w3schools.com/sql/sql_select_into.asp
    – Kling Klang
    Nov 9 at 17:10








  • 1




    @KlingKlang Because 'newtable' and 'oldtable' are from 2 totally different types of databases on two separate devices. I can't run a SQLite query from the Android app and query a MS SQL database with that query. I have to first get the data from the MS SQL Server, which will leave me with a JBDC/JTDS ResultSet object, as described in my post.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 17:27






  • 1




    @KlingKlang I could convert the ResultSet to a CSV somehow I'm sure but wouldn't that just be extra overhead? Like I said, I can parse the ResultSet into a SQLite table but it seems inefficient. I think it would be just as inefficient to go from ResultSet to CSV to SQLite table.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:13






  • 1




    @KlingKlang Though I do see a Linked Server option using an ODBC driver where I might be able to query MS SQL Server from SQLite: link and link. Maybe I'll give that a whirl.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:22






  • 1




    Or the other way around. (Throwing an INSERT INTO command from SQL Server to SQLite)
    – Kling Klang
    Nov 9 at 18:47













up vote
2
down vote

favorite









up vote
2
down vote

favorite











A little background: I am making an Android application that will remain offline most of the time. The application will need to sync with a MS SQL server database when it connects to wifi. The on-board database is SQLite. I am also very new to Java/Android development so I apologize if I'm missing the obvious somewhere.



I am able to make transactions with MS SQL server quite well using the JTDS driver however when I am querying the MS SQL server, I end up with a ResultSet object that I have to loop through one row at a time. Is there a tool/method out there than can simply insert all rows from a ResultSet into a SQLite database table?



I am currently using the standard:



ArrayList<ArrayList<String>> table = new ArrayList<>(); // a list of lists to represent a table
ArrayList<String> row = new ArrayList<>(); // a list to represent a row of the table

Class.forName("net.sourceforge.jtds.jdbc.Driver");

conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData meta = rs.getMetaData();

while (rs.next())
{
row = new ArrayList<>();

for (int i = 1; i < meta.getColumnCount() + 1; i++) // load data into List
{
row.add(rs.getString(i));
}

table.add(row);
}


From here I was going to loop through my list and add each row one by one to the SQLite database table. This seems inefficient to me. Was just looking for a better way to copy table from MS SQL Server to SQLite on an Android device.










share|improve this question















A little background: I am making an Android application that will remain offline most of the time. The application will need to sync with a MS SQL server database when it connects to wifi. The on-board database is SQLite. I am also very new to Java/Android development so I apologize if I'm missing the obvious somewhere.



I am able to make transactions with MS SQL server quite well using the JTDS driver however when I am querying the MS SQL server, I end up with a ResultSet object that I have to loop through one row at a time. Is there a tool/method out there than can simply insert all rows from a ResultSet into a SQLite database table?



I am currently using the standard:



ArrayList<ArrayList<String>> table = new ArrayList<>(); // a list of lists to represent a table
ArrayList<String> row = new ArrayList<>(); // a list to represent a row of the table

Class.forName("net.sourceforge.jtds.jdbc.Driver");

conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData meta = rs.getMetaData();

while (rs.next())
{
row = new ArrayList<>();

for (int i = 1; i < meta.getColumnCount() + 1; i++) // load data into List
{
row.add(rs.getString(i));
}

table.add(row);
}


From here I was going to loop through my list and add each row one by one to the SQLite database table. This seems inefficient to me. Was just looking for a better way to copy table from MS SQL Server to SQLite on an Android device.







java android sql-server sqlite jtds






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 17:09









Kling Klang

32.1k156287




32.1k156287










asked Nov 9 at 16:56









Smitty-Werben-Jager-Manjenson

789




789












  • Looks like a SELECT INTO could do the trick, quick and easy. w3schools.com/sql/sql_select_into.asp
    – Kling Klang
    Nov 9 at 17:10








  • 1




    @KlingKlang Because 'newtable' and 'oldtable' are from 2 totally different types of databases on two separate devices. I can't run a SQLite query from the Android app and query a MS SQL database with that query. I have to first get the data from the MS SQL Server, which will leave me with a JBDC/JTDS ResultSet object, as described in my post.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 17:27






  • 1




    @KlingKlang I could convert the ResultSet to a CSV somehow I'm sure but wouldn't that just be extra overhead? Like I said, I can parse the ResultSet into a SQLite table but it seems inefficient. I think it would be just as inefficient to go from ResultSet to CSV to SQLite table.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:13






  • 1




    @KlingKlang Though I do see a Linked Server option using an ODBC driver where I might be able to query MS SQL Server from SQLite: link and link. Maybe I'll give that a whirl.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:22






  • 1




    Or the other way around. (Throwing an INSERT INTO command from SQL Server to SQLite)
    – Kling Klang
    Nov 9 at 18:47


















  • Looks like a SELECT INTO could do the trick, quick and easy. w3schools.com/sql/sql_select_into.asp
    – Kling Klang
    Nov 9 at 17:10








  • 1




    @KlingKlang Because 'newtable' and 'oldtable' are from 2 totally different types of databases on two separate devices. I can't run a SQLite query from the Android app and query a MS SQL database with that query. I have to first get the data from the MS SQL Server, which will leave me with a JBDC/JTDS ResultSet object, as described in my post.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 17:27






  • 1




    @KlingKlang I could convert the ResultSet to a CSV somehow I'm sure but wouldn't that just be extra overhead? Like I said, I can parse the ResultSet into a SQLite table but it seems inefficient. I think it would be just as inefficient to go from ResultSet to CSV to SQLite table.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:13






  • 1




    @KlingKlang Though I do see a Linked Server option using an ODBC driver where I might be able to query MS SQL Server from SQLite: link and link. Maybe I'll give that a whirl.
    – Smitty-Werben-Jager-Manjenson
    Nov 9 at 18:22






  • 1




    Or the other way around. (Throwing an INSERT INTO command from SQL Server to SQLite)
    – Kling Klang
    Nov 9 at 18:47
















Looks like a SELECT INTO could do the trick, quick and easy. w3schools.com/sql/sql_select_into.asp
– Kling Klang
Nov 9 at 17:10






Looks like a SELECT INTO could do the trick, quick and easy. w3schools.com/sql/sql_select_into.asp
– Kling Klang
Nov 9 at 17:10






1




1




@KlingKlang Because 'newtable' and 'oldtable' are from 2 totally different types of databases on two separate devices. I can't run a SQLite query from the Android app and query a MS SQL database with that query. I have to first get the data from the MS SQL Server, which will leave me with a JBDC/JTDS ResultSet object, as described in my post.
– Smitty-Werben-Jager-Manjenson
Nov 9 at 17:27




@KlingKlang Because 'newtable' and 'oldtable' are from 2 totally different types of databases on two separate devices. I can't run a SQLite query from the Android app and query a MS SQL database with that query. I have to first get the data from the MS SQL Server, which will leave me with a JBDC/JTDS ResultSet object, as described in my post.
– Smitty-Werben-Jager-Manjenson
Nov 9 at 17:27




1




1




@KlingKlang I could convert the ResultSet to a CSV somehow I'm sure but wouldn't that just be extra overhead? Like I said, I can parse the ResultSet into a SQLite table but it seems inefficient. I think it would be just as inefficient to go from ResultSet to CSV to SQLite table.
– Smitty-Werben-Jager-Manjenson
Nov 9 at 18:13




@KlingKlang I could convert the ResultSet to a CSV somehow I'm sure but wouldn't that just be extra overhead? Like I said, I can parse the ResultSet into a SQLite table but it seems inefficient. I think it would be just as inefficient to go from ResultSet to CSV to SQLite table.
– Smitty-Werben-Jager-Manjenson
Nov 9 at 18:13




1




1




@KlingKlang Though I do see a Linked Server option using an ODBC driver where I might be able to query MS SQL Server from SQLite: link and link. Maybe I'll give that a whirl.
– Smitty-Werben-Jager-Manjenson
Nov 9 at 18:22




@KlingKlang Though I do see a Linked Server option using an ODBC driver where I might be able to query MS SQL Server from SQLite: link and link. Maybe I'll give that a whirl.
– Smitty-Werben-Jager-Manjenson
Nov 9 at 18:22




1




1




Or the other way around. (Throwing an INSERT INTO command from SQL Server to SQLite)
– Kling Klang
Nov 9 at 18:47




Or the other way around. (Throwing an INSERT INTO command from SQL Server to SQLite)
– Kling Klang
Nov 9 at 18:47

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230161%2fcopy-ms-sql-server-table-to-sqlite-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230161%2fcopy-ms-sql-server-table-to-sqlite-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word