Java Code Examples for android.database.sqlite.SQLiteDatabase.openOrCreateDatabase()

The following are Jave code examples for showing how to use openOrCreateDatabase() of the android.database.sqlite.SQLiteDatabase class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: AndroidOpen   File: DBManager.java   View Source Code Vote up 8 votes
/**
 * 通过名字或者拼音搜索
 *
 * @param keyword
 * @return
 */
public List<City> searchCity(final String keyword) {
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where name like \"%" + keyword
            + "%\" or pinyin like \"%" + keyword + "%\"", null);
    List<City> result = new ArrayList<City>();
    City city;
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(NAME));
        String pinyin = cursor.getString(cursor.getColumnIndex(PINYIN));
        city = new City(name, pinyin);
        result.add(city);
    }
    cursor.close();
    db.close();
    Collections.sort(result, new CityComparator());
    return result;
}
 
Example 2
Project: q-mail   File: LockableDatabase.java   View Source Code Vote up 6 votes
private void doOpenOrCreateDb(final File databaseFile) {
    if (StorageManager.InternalStorageProvider.ID.equals(mStorageProviderId)) {
        // internal storage
        mDb = context.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE,
                null);
    } else {
        // external storage
        mDb = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
    }
}
 
Example 3
Project: Pluto-Android   File: FinalDb.java   View Source Code Vote up 6 votes
/**
 * 在SD卡的指定目录上创建文件
 * 
 * @param sdcardPath
 * @param dbfilename
 * @return
 */
private SQLiteDatabase createDbFileOnSDCard(String sdcardPath,
		String dbfilename) {
	File dbf = new File(sdcardPath, dbfilename);
	LogUtils.info("dbf.getAbsolutePath():" + dbf.getAbsolutePath());
	if (!dbf.exists()) {
		try {
			if (dbf.createNewFile()) {
				return SQLiteDatabase.openOrCreateDatabase(dbf, null);
			}
		} catch (IOException ioex) {
			throw new DbException("数据库文件创建失败", ioex);
		}
	} else {
		return SQLiteDatabase.openOrCreateDatabase(dbf, null);
	}

	return null;
}
 
Example 4
Project: XinFramework   File: ExternalDBManager.java   View Source Code Vote up 6 votes
public SQLiteDatabase setDBManager(String packName) {
    @SuppressLint("SdCardPath") String dbPath = "/data/data/" + packName + "/databases/" + DB_NAME;
    if (!new File(dbPath).exists()) {
        try {
            FileOutputStream out = new FileOutputStream(dbPath);
            InputStream in = mContext.getAssets()
                    .open(DB_NAME);
            byte[] buffer = new byte[1024];
            int readBytes;
            while ((readBytes = in.read(buffer)) != -1)
                out.write(buffer,
                        0,
                        readBytes);
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();


        }
    }


    return SQLiteDatabase.openOrCreateDatabase(dbPath,
            null);
}
 
Example 5
Project: AndroidOpen   File: DBManager.java   View Source Code Vote up 6 votes
/**
 * 读取所有城市
 *
 * @return
 */
public List<City> getAllCities() {
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
    List<City> result = new ArrayList<City>();
    City city;
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(NAME));
        String pinyin = cursor.getString(cursor.getColumnIndex(PINYIN));
        city = new City(name, pinyin);
        result.add(city);
    }
    cursor.close();
    db.close();
    Collections.sort(result, new CityComparator());
    return result;
}
 
Example 6
Project: buildAPKsSamples   File: AbstractPicViewDatabase.java   View Source Code Vote up 6 votes
/**
 * Returns a usable database with the given name. If a database with this name
 * already exists, it is returned. Otherwise created with the given SQL create
 * query.
 */
protected static SQLiteDatabase getUsableDataBase(String dbName,
    String sqlCreateQuery) {
  File dbFile = getPathToDb(dbName);

  File fileDirectory = new File(dbFile.getParent());
  if (!fileDirectory.exists()) {
    // Make sure the path for the file exists, before creating the
    // database.
    fileDirectory.mkdirs();
  }
  Log.d(TAG, "DB Path: " + dbFile.getAbsolutePath());
  boolean initDb = !dbFile.exists();
  try {
    SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(dbFile, null);

    if (initDb) {
      result.execSQL(sqlCreateQuery);
    }

    return result;
  } catch (SQLiteException ex) {
    Log.w(TAG, "Could not open or image database.");
    return null;
  }
}
 
Example 7
Project: CitySelct   File: AllCitySqliteOpenHelper.java   View Source Code Vote up 6 votes
public void createDataBase() throws IOException {
	boolean dbExist = checkDataBase();
	if (dbExist) {
		// 数据库已存在,do nothing.
	} else {
		// 创建数据库
		try {
			File dir = new File(DB_PATH);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			File dbf = new File(DB_PATH + DB_NAME);
			if (dbf.exists()) {
				dbf.delete();
			}
			//通过openOrCreateDatabase方法将assets目录下的数据库,创建到系统默认的地方
			SQLiteDatabase.openOrCreateDatabase(dbf, null);
			// 复制asseets中的db文件到DB_PATH下
			copyDataBase();
		} catch (IOException e) {
			throw new Error("数据库创建失败");
		}
	}
}
 
Example 8
Project: boohee_v5.6   File: ay.java   View Source Code Vote up 6 votes
private void ce() {
    SQLiteDatabase openOrCreateDatabase;
    try {
        openOrCreateDatabase = SQLiteDatabase.openOrCreateDatabase(this.iB, null);
    } catch (Exception e) {
        openOrCreateDatabase = null;
    }
    if (openOrCreateDatabase != null) {
        long queryNumEntries = DatabaseUtils.queryNumEntries(openOrCreateDatabase, "wof");
        long queryNumEntries2 = DatabaseUtils.queryNumEntries(openOrCreateDatabase, "bdcltb09");
        boolean z = queryNumEntries > 10000;
        boolean z2 = queryNumEntries2 > 10000;
        if (z || z2) {
            new a().execute(new Boolean[]{Boolean.valueOf(z), Boolean.valueOf(z2)});
        }
        openOrCreateDatabase.close();
    }
}
 
Example 9
Project: boohee_v5.6   File: ay.java   View Source Code Vote up 6 votes
private void cg() {
    try {
        File file = new File(I);
        File file2 = new File(this.iB);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (!file2.exists()) {
            file2.createNewFile();
        }
        if (file2.exists()) {
            SQLiteDatabase openOrCreateDatabase = SQLiteDatabase.openOrCreateDatabase(file2, null);
            openOrCreateDatabase.execSQL("CREATE TABLE IF NOT EXISTS bdcltb09(id CHAR(40) PRIMARY KEY,time DOUBLE,tag DOUBLE, type DOUBLE , ac INT);");
            openOrCreateDatabase.execSQL("CREATE TABLE IF NOT EXISTS wof(id CHAR(15) PRIMARY KEY,mktime DOUBLE,time DOUBLE, ac INT, bc INT, cc INT);");
            openOrCreateDatabase.setVersion(1);
            openOrCreateDatabase.close();
        }
    } catch (Exception e) {
    }
}
 
Example 10
Project: StudentInfoClient   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 初始化在线数据库
    // OnlineDatabase.connect(this.getResources().getString(R.string.DataBaseUrl));
    LocalDatabase=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/studentinfo.db3",null);
    background_task task=new background_task(this);
    task.execute();
}
 
Example 11
Project: GitHub   File: DbManagerImpl.java   View Source Code Vote up 5 votes
private SQLiteDatabase openOrCreateDatabase(DaoConfig config) {
    SQLiteDatabase result = null;

    File dbDir = config.getDbDir();
    if (dbDir != null && (dbDir.exists() || dbDir.mkdirs())) {
        File dbFile = new File(dbDir, config.getDbName());
        result = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
    } else {
        result = x.app().openOrCreateDatabase(config.getDbName(), 0, null);
    }
    return result;
}
 
Example 12
Project: SQLite2XL   File: SQLiteToExcel.java   View Source Code Vote up 5 votes
public SQLiteToExcel(Context context, String dbName, String exportPath) {
    mContext = context;
    mDbName = dbName;
    mExportPath = exportPath;
    try {
        database = SQLiteDatabase.openOrCreateDatabase(mContext.getDatabasePath(mDbName).getAbsolutePath(), null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Project: SimpleDbFrame   File: BaseDaoFactory.java   View Source Code Vote up 5 votes
private BaseDaoFactory() {
    if (TextUtils.isEmpty(mDbPath)) {
        throw new RuntimeException("在使用BaseDaoFactory之前,请调用BaseDaoFactory.init()初始化好数据库路径。");
    }
    // 打开数据库,得到数据库对象
    mDatabase = SQLiteDatabase.openOrCreateDatabase(mDbPath, null);
}
 
Example 14
Project: boohee_v5.6   File: ay.java   View Source Code Vote up 5 votes
private void r(String str) {
    SQLiteDatabase sQLiteDatabase = null;
    if (str != null && !str.equals(this.iG)) {
        try {
            sQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(this.iB, null);
        } catch (Exception e) {
        }
        if (sQLiteDatabase == null || str == null) {
            this.iy = false;
            return;
        }
        this.iy = false;
        try {
            Cursor rawQuery = sQLiteDatabase.rawQuery("select * from bdcltb09 where id = \"" + str + "\";", null);
            this.iG = str;
            this.ip = System.currentTimeMillis();
            if (rawQuery != null) {
                if (rawQuery.moveToFirst()) {
                    this.iv = rawQuery.getDouble(1) - 1235.4323d;
                    this.iz = rawQuery.getDouble(2) - 4326.0d;
                    this.iu = rawQuery.getDouble(3) - 2367.3217d;
                    this.iy = true;
                }
                rawQuery.close();
            }
        } catch (Exception e2) {
        }
        sQLiteDatabase.close();
    }
}
 
Example 15
Project: SQLite2XL   File: ExcelToSQLite.java   View Source Code Vote up 5 votes
public ExcelToSQLite(Context context, String dbName, boolean dropTable) {
    mContext = context;
    mDbName = dbName;
    this.dropTable = dropTable;
    try {
        database = SQLiteDatabase.openOrCreateDatabase(mContext.getDatabasePath(mDbName).getAbsolutePath(), null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 16
Project: Android-Fitness   File: DBManager.java   View Source Code Vote up 5 votes
public SQLiteDatabase openDatabase() {
	try {
		// ���food.db�ļ��ľ���·��
		String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
		File dir = new File(DATABASE_PATH);
		// ���/sdcard/FitnessĿ¼�д��ڣ��������Ŀ¼
		if (!dir.exists())
			dir.mkdir();
		// �����/sdcard/FitnessĿ¼�в�����food.db�ļ������resawĿ¼�и�������ļ���SD����Ŀ¼/sdcard/Fitness
		if (!(new File(databaseFilename)).exists()) {
			// ��÷�װfood.db�ļ���InputStream����
			InputStream is = context.getResources().openRawResource(R.raw.fitness);
			FileOutputStream fos = new FileOutputStream(databaseFilename);
			byte[] buffer = new byte[BUFF_SIZE];
			int count = 0;
			// ��ʼ����food.db�ļ�
			while ((count = is.read(buffer)) > 0) {
				fos.write(buffer, 0, count);
			}

			fos.close();
			is.close();
		}
		// ��/sdcard/FitnessĿ¼�е�food.db�ļ�
		SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
		return database;
	} catch (Exception e) {
	}
	return null;
}
 
Example 17
Project: boohee_v5.6   File: ay.java   View Source Code Vote up 5 votes
protected Boolean a(Boolean... boolArr) {
    SQLiteDatabase sQLiteDatabase = null;
    if (boolArr.length != 4) {
        return Boolean.valueOf(false);
    }
    try {
        sQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(this.a.iB, null);
    } catch (Exception e) {
    }
    if (sQLiteDatabase == null) {
        return Boolean.valueOf(false);
    }
    int currentTimeMillis = (int) (System.currentTimeMillis() >> 28);
    try {
        sQLiteDatabase.beginTransaction();
        if (boolArr[0].booleanValue()) {
            try {
                sQLiteDatabase.execSQL("delete from wof where ac < " + (currentTimeMillis - 35));
            } catch (Exception e2) {
            }
        }
        if (boolArr[1].booleanValue()) {
            try {
                sQLiteDatabase.execSQL("delete from bdcltb09 where ac is NULL or ac < " + (currentTimeMillis - 130));
            } catch (Exception e3) {
            }
        }
        sQLiteDatabase.setTransactionSuccessful();
        sQLiteDatabase.endTransaction();
        sQLiteDatabase.close();
    } catch (Exception e4) {
    }
    return Boolean.valueOf(true);
}
 
Example 18
Project: letv   File: FabricContext.java   View Source Code Vote up 4 votes
@TargetApi(11)
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) {
    return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name).getPath(), factory, errorHandler);
}
 
Example 19
Project: MVVMFrames   File: DBContextWrapper.java   View Source Code Vote up 2 votes
/**
 * Android2.3及以下会调用这个方法
 *
 * @param name
 * @param mode
 * @param factory
 */
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
    return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), factory);
}
 
Example 20
Project: MVVMFrames   File: DBContextWrapper.java   View Source Code Vote up 2 votes
/**
 * Android 4.0会调用此方法获取数据库
 *
 * @param name
 * @param mode
 * @param factory
 * @param errorHandler
 */
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
    return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), factory);
}