Skip to content

Commit 9c5ab57

Browse files
committed
Add New Column from Excel while importing Excel(Issue No #4). Library Size Reduced.
1 parent 47a4259 commit 9c5ab57

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

app/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ android {
2020

2121
dependencies {
2222
compile fileTree(dir: 'libs', include: ['*.jar'])
23-
// compile project(path: ':library')
24-
compile 'com.ajts.androidmads.SQLite2Excel:library:1.0.1'
23+
compile project(path: ':library')
24+
// compile 'com.ajts.androidmads.SQLite2Excel:library:1.0.1'
2525
compile 'com.android.support:appcompat-v7:25.2.0'
2626
compile 'com.android.support:design:25.2.0'
2727
}
28+
repositories {
29+
mavenCentral()
30+
}

app/src/main/java/com/ajts/androidmads/sqlite2xlDemo/Excel2SQLiteActivity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public void onClick(final View view) {
4646
return;
4747
}
4848
dbQueries.open();
49-
ExcelToSQLite excelToSQLite = new ExcelToSQLite(getApplicationContext(), DBHelper.DB_NAME);
49+
// Is used to import data from excel without dropping table
50+
// ExcelToSQLite excelToSQLite = new ExcelToSQLite(getApplicationContext(), DBHelper.DB_NAME);
51+
52+
// if you want to add column in excel and import into DB, you must drop the table
53+
ExcelToSQLite excelToSQLite = new ExcelToSQLite(getApplicationContext(), DBHelper.DB_NAME, false);
5054
// Import EXCEL FILE to SQLite
5155
excelToSQLite.importFromFile(directory_path, new ExcelToSQLite.ImportListener() {
5256
@Override

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ buildscript {
88
classpath 'com.android.tools.build:gradle:2.3.1'
99
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
1010
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
11-
1211
// NOTE: Do not place your application dependencies here; they belong
1312
// in the individual module build.gradle files
1413
}

library/build.gradle

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'com.jfrog.bintray'
33
apply plugin: 'com.github.dcendents.android-maven'
44

55
group = 'com.ajts.androidmads.SQLite2Excel'
6-
version = '1.0.1'
6+
version = '1.0.2'
77

88
android {
99
compileSdkVersion 25
@@ -24,8 +24,7 @@ android {
2424
}
2525

2626
dependencies {
27-
compile fileTree(include: ['*.jar'], dir: 'libs')
28-
compile files('libs/poi.jar')
27+
compile 'org.apache.poi:poi:3.16'
2928
}
3029

3130
def siteUrl = 'https://github.com/androidmads/SQLite2XL' // Homepage URL of the library
@@ -34,6 +33,27 @@ def gitUrl = 'https://github.com/androidmads/SQLite2XL.git' // Git URL
3433
Properties properties = new Properties()
3534
properties.load(project.rootProject.file('local.properties').newDataInputStream())
3635

36+
bintray {
37+
user = properties.getProperty("bintray.user")
38+
key = properties.getProperty("bintray.apikey")
39+
configurations = ['archives']
40+
pkg {
41+
repo = 'maven'
42+
name = 'com.ajts.androidmads.SQLite2Excel'
43+
44+
version {
45+
name = '1.0.2'
46+
desc = 'Library to Convert SQlite Data to Excel'
47+
released = new Date()
48+
vcsTag = '1.0.2'
49+
}
50+
51+
licenses = ['Apache-2.0']
52+
vcsUrl = gitUrl
53+
websiteUrl = siteUrl
54+
}
55+
}
56+
3757
install {
3858
repositories.mavenInstaller {
3959
// This generates POM.xml with proper parameters
@@ -68,26 +88,6 @@ install {
6888
}
6989
}
7090
}
71-
bintray {
72-
user = properties.getProperty("bintray.user")
73-
key = properties.getProperty("bintray.apikey")
74-
configurations = ['archives']
75-
pkg {
76-
repo = 'maven'
77-
name = 'com.ajts.androidmads.SQLite2Excel'
78-
79-
version {
80-
name = '1.0.1'
81-
desc = 'Library to Convert SQlite Data to Excel'
82-
released = new Date()
83-
vcsTag = '1.0.1'
84-
}
85-
86-
licenses = ['Apache-2.0']
87-
vcsUrl = gitUrl
88-
websiteUrl = siteUrl
89-
}
90-
}
9191

9292
task generateSourcesJar(type: Jar) {
9393
from android.sourceSets.main.java.srcDirs
@@ -107,5 +107,4 @@ task generateJavadocsJar(type: Jar) {
107107
artifacts {
108108
archives generateSourcesJar
109109
archives generateJavadocsJar
110-
}
111-
110+
}

library/libs/poi.jar

-2.38 MB
Binary file not shown.

library/src/main/java/com/ajts/androidmads/library/ExcelToSQLite.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.ContentValues;
44
import android.content.Context;
5+
import android.database.Cursor;
56
import android.database.sqlite.SQLiteDatabase;
67
import android.os.Handler;
78
import android.os.Looper;
@@ -25,6 +26,7 @@ public class ExcelToSQLite {
2526
private Context mContext;
2627
private SQLiteDatabase database;
2728
private String mDbName;
29+
private boolean dropTable = false;
2830

2931
public ExcelToSQLite(Context context, String dbName) {
3032
mContext = context;
@@ -36,6 +38,17 @@ public ExcelToSQLite(Context context, String dbName) {
3638
}
3739
}
3840

41+
public ExcelToSQLite(Context context, String dbName, boolean dropTable) {
42+
mContext = context;
43+
mDbName = dbName;
44+
this.dropTable = dropTable;
45+
try {
46+
database = SQLiteDatabase.openOrCreateDatabase(mContext.getDatabasePath(mDbName).getAbsolutePath(), null);
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
3952
public void importFromAsset(final String assetFileName, final ImportListener listener) {
4053
if (listener != null) {
4154
listener.onStart();
@@ -135,7 +148,22 @@ private void createTable(Sheet sheet) {
135148
columns.add(rowHeader.getCell(i).getStringCellValue());
136149
}
137150
createTableSql.append(")");
151+
152+
if (dropTable)
153+
database.execSQL("DROP TABLE IF EXISTS " + sheet.getSheetName());
154+
138155
database.execSQL(createTableSql.toString());
156+
157+
for (String column : columns) {
158+
Cursor cursor = database.rawQuery("SELECT * FROM " + sheet.getSheetName(), null); // grab cursor for all data
159+
int deleteStateColumnIndex = cursor.getColumnIndex(column); // see if the column is there
160+
if (deleteStateColumnIndex < 0) {
161+
String type = "TEXT";
162+
// missing_column not there - add it
163+
database.execSQL("ALTER TABLE " + sheet.getSheetName() + " ADD COLUMN " + column + " " + type + " NULL;");
164+
}
165+
}
166+
139167
while (rit.hasNext()) {
140168
Row row = rit.next();
141169
ContentValues values = new ContentValues();
@@ -146,9 +174,9 @@ private void createTable(Sheet sheet) {
146174
values.put(columns.get(n), row.getCell(n).getStringCellValue());
147175
}
148176
}
149-
long result = database.insert(sheet.getSheetName(), null, values);
177+
long result = database.insertWithOnConflict(sheet.getSheetName(), null, values, SQLiteDatabase.CONFLICT_IGNORE);
150178
if (result < 0) {
151-
throw new RuntimeException("insert value failed!");
179+
throw new RuntimeException("Insert value failed!");
152180
}
153181
}
154182
}

0 commit comments

Comments
 (0)