当前位置:   article > 正文

android studio输入数据库,android studio中的数据库

androidstudio如何手动输入数据库

我是一名Windows手机开发者,最近我开始使用android studio开发Android应用程序.

我需要创建一个数据库并在其中存储值并在屏幕上检索更新的值,所以我需要帮助:

>创建数据库.

>如何在屏幕上显示数据库中的值?

解决方法:

要创建数据库,您需要扩展SQLiteOpenHelper并需要一个带Context的构造函数.

假设您将此类命名为DBOperator.表创建过程看起来像这样,

public class DbOperator extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "DB_NAME";

protected static final String FIRST_TABLE_NAME = "FIRST_TABLE";

protected static final String SECOND_TABLE_NAME = "SECOND_TABLE";

public static final String CREATE_FIRST_TABLE = "create table if not exists "

+ FIRST_TABLE_NAME

+ " ( _id integer primary key autoincrement, COL1 TEXT NOT NULL, COL2 TEXT NOT NULL,COL3 TEXT, COL4 int, COL5 TEXT,"

+ "COL6 TEXT,COL7 REAL, COL8 INTEGER,COL9 TEXT not null);";

public static final String CREATE_SECOND_TABLE = "create table if not exists "

+ SECOND_TABLE_NAME+.........

public DbOperator(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_SFIRST_TABLE);

db.execSQL(CREATE_SECOND_TABLE);

//db.close();

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

//THIS WILL BE EXECUTED WHEN YOU UPDATED VERSION OF DATABASE_VERSION

//YOUR DROP AND CREATE QUERIES

}

}

现在你的数据操作类(添加,删除,更新)看起来像这样,

public class FirstTableDML extends DbOperator {

public FirstTableDML(Context context) {

super(context);

}

private static final String COL_ID = "_id";

private static final String COL1 = "COL1";

private static final String COL2 = "COL2";

........

.......

public void deleteFirstTableDataList(List firstTableDataList) {

for (FirstTableData data : firstTableDataList)

deleteFirstTableDetailData(data);

}

public void deleteFirstTableDetailData(FirstTableData item) {

SQLiteDatabase db = this.getWritableDatabase();

db.delete(FIRST_TABLE_NAME, item.getId() + "=" + COL_ID, null);

db.close();

}

/**this method retrieves all the records from table and returns them as list of

FirstTableData types. Now you use this list to display detail on your screen as per your

requirements.

*/

public List< FirstTableData > getFirstTableDataList() {

List< FirstTableData > firstTableDataList = new ArrayList< FirstTableData >();

String refQuery = "Select * From " + FIRST_TABLE_NAME;

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(refQuery, null);

try {

if (cursor.moveToFirst()) {

do {

FirstTableData itemData = new FirstTableData();

itemData.setId(cursor.getInt(0));

itemData.setCol1(cursor.getString(1));

itemData.setCol2(cursor.getInt(2));

.....

.....

firstTableDataList.add(itemData);

} while (cursor.moveToNext());

}

} finally {

db.close();

}

Collections.sort(itemDataList);

return itemDataList;

}

public int addFirstTableData(FirstTableData data) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(COL1, data.getCol1());

values.put(COL2, data.getCol2());

.....

.....

long x=db.insert(FIRST_TABLE_NAME, null, values);

db.close();

return (int)x;

}

public void updateItemDetailData(FirstTableData data) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(COL1, data.getCol1());

values.put(COL2, data.getCol2());

values.put(COL3, data.getCol3());

.....

.....

db.update(FIRST_TABLE_NAME, values, COL_ID + "=" + data.getId(), null);

db.close();

}

}

P.S:*数据类是表示相应表的POJO数据类.

既然你说你并不是全新的,我没有提供任何帮助评论,因为大多数方法名称都是自我解释的.

希望它能帮助您入门.

标签:database,android,sqlite,android-studio,database-design

来源: https://codeday.me/bug/20190712/1443707.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号