Java Code Examples for android.database.SQLException#printStackTrace()

The following examples show how to use android.database.SQLException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseHandler.java    From repay-android with Apache License 2.0 6 votes vote down vote up
/**
 * Replace friend data in database with that passed in here
 * @param friend
 */
public void updateFriendRecord(Friend friend) throws SQLException, NullPointerException{
	ContentValues values = new ContentValues();
	values.put(Names.F_REPAYID, friend.getRepayID());
	if(friend.getLookupURI()!=null){
		values.put(Names.F_LOOKUPURI, friend.getLookupURI());
	}
	else{
		values.putNull(Names.F_LOOKUPURI);
	}
	values.put(Names.F_NAME, friend.getName());
	values.put(Names.F_DEBT, friend.getDebt().toString());
	SQLiteDatabase db = this.getWritableDatabase();
	try {
		db.update(Names.F_TABLENAME, values, Names.F_REPAYID+"=?",
				new String[]{friend.getRepayID()});
	} catch (SQLException e) {
		Log.e(TAG, e.getMessage());
		e.printStackTrace();
		throw e;
	} finally {
		db.close();
	}
}
 
Example 2
Source File: OfflineDatabaseHandler.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase db) {
    Log.i(TAG, "onCreate() called... Setting up application's database.");
    // Create The table(s)
    String metadata = "CREATE TABLE " + TABLE_METADATA + " (" + FIELD_METADATA_NAME + " TEXT UNIQUE, " + FIELD_METADATA_VALUE + " TEXT);";
    String resources = "CREATE TABLE " + TABLE_RESOURCES + " (" + FIELD_RESOURCES_URL + " TEXT UNIQUE, " + FIELD_RESOURCES_DATA + " BLOB, " + FIELD_RESOURCES_STATUS + " TEXT);";

    db.beginTransaction();

    try {
        db.execSQL(metadata);
        db.execSQL(resources);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
        Log.e(TAG, "Error creating database: " + e.toString());
        e.printStackTrace();
    } finally {
        db.endTransaction();
    }
}
 
Example 3
Source File: GanHuoContentProvider.java    From base-module with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    Cursor cursor = null;
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    String tableName = DatabaseManager.matchUri(uri);
    try {
        db.beginTransaction();
        cursor = db.query(tableName, projection, selection, selectionArgs, null, null, sortOrder);
    } catch (SQLException e) {
        Log.e(TAG, "query " + tableName + " error: " + e);
        e.printStackTrace();
    } finally {
        db.endTransaction();
    }
    return cursor;
}
 
Example 4
Source File: GanHuoContentProvider.java    From base-module with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    Uri returnUri = null;
    SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
    String tableName = DatabaseManager.matchUri(uri);
    long rowId = -1;
    try {
        db.beginTransaction();
        rowId = db.replace(tableName, null, values);
        return rowId > 0
                ? ContentUris.withAppendedId(uri, rowId)
                : ContentUris.withAppendedId(uri, -1);
    } catch (SQLException e) {
        Log.e(TAG, "insert " + tableName + " error: " + e);
        e.printStackTrace();
    } finally {
        db.endTransaction();
    }
    return returnUri;
}
 
Example 5
Source File: FavoriteDao.java    From GreenDamFileExploere with Apache License 2.0 5 votes vote down vote up
/**
 * 查询所有收藏内容
 * 
 * @return
 */
public List<Favorite> findAllFavorite() {
    List<Favorite> favorites = new ArrayList<Favorite>();
    favorites.clear();
    SQLiteDatabase db = null;
    Cursor c = null;
    try {
        db = mAppNameDbHelper.openDatabase();
        c = db.rawQuery(SQL_FIND_ALL_FAVORITE, null);

        while (c.moveToNext()) {
            String path = c.getString(c.getColumnIndex("path"));
            String name = c.getString(c.getColumnIndex("name"));
            String appName = c.getString(c.getColumnIndex("app_name"));
            int fileType = c.getInt(c.getColumnIndex("file_type"));
            long date = c.getLong(c.getColumnIndex("favorite_time"));
            long size = c.getLong(c.getColumnIndex("size"));
            String extra = c.getString(c.getColumnIndex("extra"));
            Favorite favorite = new Favorite(path, name, appName, fileType, date, size, extra);
            favorites.add(favorite);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return favorites;
}
 
Example 6
Source File: ContactDao.java    From monolog-android with MIT License 5 votes vote down vote up
public static void insert(UserBean msg, String ownerid) {

        DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
                ContactTable.TABLE_NAME);
        final int nameCol = ih.getColumnIndex(ContactTable.NAME);
        final int avatarCol = ih.getColumnIndex(ContactTable.AVATAR);
        final int uidCol = ih.getColumnIndex(ContactTable.UID);
        final int genderCol = ih.getColumnIndex(ContactTable.GENDER);
        final int ownerCol = ih.getColumnIndex(ContactTable.OWNERID);
        final int rightCountCol = ih.getColumnIndex(ContactTable.RIGHT_COUNT);
        final int missCountCol = ih.getColumnIndex(ContactTable.MISS_COUNT);
        final int statueCountCol = ih.getColumnIndex(ContactTable.STATUE_COUNT);
        try {
            getWsd().beginTransaction();
            ih.prepareForInsert();
            ih.bind(nameCol, msg.getName());
            ih.bind(avatarCol, msg.getAvatar());
            ih.bind(uidCol, msg.getId());
            ih.bind(genderCol, msg.getGender());
            ih.bind(rightCountCol, msg.getRight_count());
            ih.bind(missCountCol, msg.getMiss_count());
            ih.bind(statueCountCol, msg.getStatue_count());
            ih.bind(ownerCol, ownerid);

            ih.execute();
            getWsd().setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            getWsd().endTransaction();
            ih.close();
        }
    }
 
Example 7
Source File: FavoriteDao.java    From GreenDamFileExploere with Apache License 2.0 5 votes vote down vote up
public Favorite findFavoriteByFullPath(String canonicalPath) {
    Favorite favorite = null;

    SQLiteDatabase db = null;
    Cursor c = null;
    try {
        db = mAppNameDbHelper.openDatabase();
        c = db.rawQuery(SQL_FIND_FAVORITE_BY_PATH, new String[] { canonicalPath });

        if (c.moveToLast()) {
            String path = c.getString(c.getColumnIndex("path"));
            String name = c.getString(c.getColumnIndex("name"));
            String appName = c.getString(c.getColumnIndex("app_name"));
            int fileType = c.getInt(c.getColumnIndex("file_type"));
            long date = c.getLong(c.getColumnIndex("favorite_time"));
            long size = c.getLong(c.getColumnIndex("size"));
            String extra = c.getString(c.getColumnIndex("extra"));
            favorite = new Favorite(path, name, appName, fileType, date, size, extra);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        mAppNameDbHelper.closeDatabase();
        if (c != null) {
            c.close();
        }
    }
    return favorite;
}
 
Example 8
Source File: LauncherProvider.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
private boolean addIntegerColumn(SQLiteDatabase db, String columnName, long defaultValue) {
    db.beginTransaction();
    try {
        db.execSQL("ALTER TABLE favorites ADD COLUMN "
                + columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";");
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        ex.printStackTrace();
        return false;
    } finally {
        db.endTransaction();
    }
    return true;
}
 
Example 9
Source File: LauncherProvider.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) {
    db.beginTransaction();
    try {
        if (addRankColumn) {
            // Insert new column for holding rank
            db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;");
        }

        // Get a map for folder ID to folder width
        Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites"
                + " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)"
                + " GROUP BY container;",
                new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)});

        while (c.moveToNext()) {
            db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE "
                    + "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;",
                    new Object[] {c.getLong(1) + 1, c.getLong(0)});
        }

        c.close();
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        // Old version remains, which means we wipe old data
        ex.printStackTrace();
        return false;
    } finally {
        db.endTransaction();
    }
    return true;
}
 
Example 10
Source File: ContactDao.java    From monolog-android with MIT License 5 votes vote down vote up
public static void insertList(ArrayList<UserBean> list, String ownerid) {

        if (list == null || list.size() == 0) {
            return;
        }
        DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
                ContactTable.TABLE_NAME);
        final int nameCol = ih.getColumnIndex(ContactTable.NAME);
        final int avatarCol = ih.getColumnIndex(ContactTable.AVATAR);
        final int uidCol = ih.getColumnIndex(ContactTable.UID);
        final int genderCol = ih.getColumnIndex(ContactTable.GENDER);
        final int ownerCol = ih.getColumnIndex(ContactTable.OWNERID);
        final int rightCountCol = ih.getColumnIndex(ContactTable.RIGHT_COUNT);
        final int missCountCol = ih.getColumnIndex(ContactTable.MISS_COUNT);
        final int statueCountCol = ih.getColumnIndex(ContactTable.STATUE_COUNT);
        try {
            getWsd().beginTransaction();
            for (int i = 0; i < list.size(); i++) {
                UserBean msg = list.get(i);
                ih.prepareForInsert();
                ih.bind(nameCol, msg.getName());
                ih.bind(avatarCol, msg.getAvatar());
                ih.bind(uidCol, msg.getId());
                ih.bind(genderCol, msg.getGender());
                ih.bind(rightCountCol, msg.getRight_count());
                ih.bind(missCountCol, msg.getMiss_count());
                ih.bind(statueCountCol, msg.getStatue_count());
                ih.bind(ownerCol, ownerid);
                ih.execute();
            }
            getWsd().setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            getWsd().endTransaction();
            ih.close();
        }
    }
 
Example 11
Source File: DatabaseFragment.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public int getCount() {
    try {
        return dao.queryForAll().size();
    } catch (SQLException e) {
        e.printStackTrace();
        android.util.Log.e(TAG, "DataService getCount fail!");
    }
    return -1;
}
 
Example 12
Source File: MessageDatabaseAdapter.java    From Chimee with MIT License 5 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase db) {

	try {
		db.execSQL(CREATE_FAVORITE_TABLE);
		db.execSQL(CREATE_HISTORY_TABLE);
		insertDefaultInitialData(db);
	} catch (SQLException e) {
		e.printStackTrace();
	}

}
 
Example 13
Source File: DatabaseFragment.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
public List<Data> getAllList() {
    try {
        return dao.queryForAll();
    } catch (SQLException e) {
        e.printStackTrace();
        android.util.Log.e(TAG, "DataService getAllList fail!");
    }
    return null;
}
 
Example 14
Source File: FileAppNameDao.java    From GreenDamFileExploere with Apache License 2.0 5 votes vote down vote up
/**
 * 查找app名字
 * 
 * @param canonicalPath
 * @return
 */
public String findAppName(String canonicalPath) {
    String appName = "";
    if (canonicalPath.contains(ResourceManager.mExternalStoragePath)) {
        canonicalPath = canonicalPath.substring(ResourceManager.mExternalStoragePath.length());
    }

    if (!needToFindDatabase(canonicalPath, "/", 3)) {
        return appName;
    }

    SQLiteDatabase db = null;
    Cursor c = null;
    try {
        db = mAppNameDbHelper.openDatabase();
        // System.out.println("---->canonicalPath:" + canonicalPath);
        c = db.rawQuery(SQL_FIND_APP_NAME, new String[] { canonicalPath });
        if (c.moveToLast()) {
            appName = c.getString(0);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (c != null) {
            c.close();
        }
    }

    return appName;

}
 
Example 15
Source File: DatabaseTest.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
public List<Data> getAllList() {
    try {
        return dao.queryForAll();
    } catch (SQLException e) {
        e.printStackTrace();
        android.util.Log.e(TAG, "DataService getAllList fail!");
    }
    return null;
}
 
Example 16
Source File: DatabaseTest.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public int getCount() {
    try {
        return dao.queryForAll().size();
    } catch (SQLException e) {
        e.printStackTrace();
        android.util.Log.e(TAG, "DataService getCount fail!");
    }
    return -1;
}
 
Example 17
Source File: DatabaseTest.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public Data find(Integer id) {
    try {
        return dao.queryForId(id);
    } catch (SQLException e) {
        e.printStackTrace();
        android.util.Log.e(TAG, "DataService find fail!");
    }
    return null;
}
 
Example 18
Source File: PnDb.java    From PressureNet with GNU General Public License v3.0 5 votes vote down vote up
public boolean addForecastLocationArrayList(ArrayList<ForecastLocation> locationList) {
	
	try {
		mDB.beginTransaction();
	} catch(SQLiteDatabaseLockedException sqldble) {
		// This try/catch block is a bad hack. Refactor db usaage to use only one lock
		// regardless of the thread
		
	}
	
	String insertSQL = "INSERT INTO "
			+ FORECAST_LOCATIONS
			+ " ("
			+ KEY_FORECAST_LOCATION_ID
			+ ", "
			+ KEY_FORECAST_LATITUDE
			+ ", "
			+ KEY_FORECAST_LONGITUDE
			+ ") values (?, ?, ?)";

	try {
		SQLiteStatement insert = mDB.compileStatement(insertSQL);
		for (ForecastLocation location : locationList) {
			insert.bindString(1, location.getLocationID());
			insert.bindDouble(2, location.getLatitude());
			insert.bindDouble(3, location.getLongitude());
			insert.executeInsert();
		}

		mDB.setTransactionSuccessful();
	} catch (SQLException sqle) {
		sqle.printStackTrace();
	} finally {
		mDB.endTransaction();
	}

	return true;
}
 
Example 19
Source File: PnDb.java    From PressureNet with GNU General Public License v3.0 4 votes vote down vote up
public boolean addTemperatureForecastArrayList(ArrayList<TemperatureForecast> tempForecasts) {
	
	try {
		mDB.beginTransaction();
	} catch(SQLiteDatabaseLockedException sqldble) {
		// This try/catch block is a bad hack. Refactor db usaage to use only one lock
		// regardless of the thread
		
	}
	
	String insertSQL = "INSERT INTO "
			+ TEMPERATURES
			+ " ("
			+ KEY_FORECAST_LOCATION_ID
			+ ", "
			+ KEY_TEMP_FORECAST_START_TIME
			+ ", "
			+ KEY_TEMP_FORECAST_HOUR
			+ ", "
			+ KEY_TEMP_FORECAST_SCALE
			+ ", "
			+ KEY_TEMP_FORECAST_VALUE
			+ ") values (?, ?, ?, ?, ?)";

	try {
		SQLiteStatement insert = mDB.compileStatement(insertSQL);
		for (TemperatureForecast temp : tempForecasts) {
			insert.bindString(1, temp.getLocationID());
			insert.bindString(2, temp.getStartTime());
			insert.bindLong(3, temp.getForecastHour());
			insert.bindLong(4, temp.getScale());
			insert.bindDouble(5, temp.getTemperatureValue());
			insert.executeInsert();
		}

		mDB.setTransactionSuccessful();
	} catch (SQLException sqle) {
		sqle.printStackTrace();
	} finally {
		mDB.endTransaction();
	}

	return true;
}
 
Example 20
Source File: SchemaGenerator.java    From ApkTrack with GNU General Public License v3.0 4 votes vote down vote up
private void createTable(Class<?> table, SQLiteDatabase sqLiteDatabase) {
    Log.i(SUGAR, "Create table if not exists");
    List<Field> fields = ReflectionUtil.getTableFields(table);
    String tableName = NamingHelper.toSQLName(table);
    StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
    sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT ");

    for (Field column : fields) {
        String columnName = NamingHelper.toSQLName(column);
        String columnType = QueryBuilder.getColumnType(column.getType());

        if (columnType != null) {
            if (columnName.equalsIgnoreCase("Id")) {
                continue;
            }

            if (column.isAnnotationPresent(Column.class)) {
                Column columnAnnotation = column.getAnnotation(Column.class);
                columnName = columnAnnotation.name();

                sb.append(", ").append(columnName).append(" ").append(columnType);

                if (columnAnnotation.notNull()) {
                    if (columnType.endsWith(NULL)) {
                        sb.delete(sb.length() - 5, sb.length());
                    }
                    sb.append(NOT_NULL);
                }

                if (columnAnnotation.unique()) {
                    sb.append(UNIQUE);
                }

            } else {
                sb.append(", ").append(columnName).append(" ").append(columnType);

                if (column.isAnnotationPresent(NotNull.class)) {
                    if (columnType.endsWith(NULL)) {
                        sb.delete(sb.length() - 5, sb.length());
                    }
                    sb.append(NOT_NULL);
                }

                if (column.isAnnotationPresent(Unique.class)) {
                    sb.append(UNIQUE);
                }
            }
        }
    }

    sb.append(" ) ");
    Log.i(SUGAR, "Creating table " + tableName);

    if (!sb.toString().isEmpty()) {
        try {
            sqLiteDatabase.execSQL(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}