Android app 连接数据库的代码
时间: 2024-02-29 21:52:18 浏览: 128
Android连接数据库源码
3星 · 编辑精心推荐
连接数据库的代码需要在 Android 应用程序的后端服务器上运行。常用的数据库包括 MySQL, SQLite, PostgreSQL 等。以下是一个使用 SQLite 数据库的示例代码,供参考:
1. 添加依赖库
在 app 的 build.gradle 文件中添加以下依赖库:
```
dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.0'
}
```
2. 创建数据库和表
```
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "mytable";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_AGE + " INTEGER)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
```
3. 连接数据库
```
public class DBManager {
private DBHelper dbHelper;
private Context context;
private SQLiteDatabase database;
public DBManager(Context c) {
context = c;
}
public DBManager open() throws SQLException {
dbHelper = new DBHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void insert(String name, int age) {
ContentValues contentValue = new ContentValues();
contentValue.put(DBHelper.COLUMN_NAME, name);
contentValue.put(DBHelper.COLUMN_AGE, age);
database.insert(DBHelper.TABLE_NAME, null, contentValue);
}
public Cursor fetch() {
String[] columns = new String[] { DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME, DBHelper.COLUMN_AGE };
Cursor cursor = database.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
}
```
4. 在 Activity 中使用
```
public class MainActivity extends AppCompatActivity {
private DBManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbManager = new DBManager(this);
dbManager.open();
dbManager.insert("John", 30);
Cursor cursor = dbManager.fetch();
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME));
int age = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_AGE));
Log.d("TAG", "Name: " + name + ", Age: " + age);
} while (cursor.moveToNext());
}
dbManager.close();
}
}
```
注意,以上代码仅供参考,需要根据自己的实际需求进行修改和适配。同时,在实际开发中,为了保证数据安全和隐私,需要对数据库连接进行加密和权限控制。
阅读全文