在 Android Studio 中插入数据

Posted

技术标签:

【中文标题】在 Android Studio 中插入数据【英文标题】:data insert in Android Studio 【发布时间】:2021-11-28 01:13:36 【问题描述】:

我实际上正在研究 SQLite 数据库。我试图在我的数据库脚本中插入数据,以便稍后在 recyclerview 中显示它,而不是通过我在应用程序的前端输入它,而是直接在脚本中输入。我该怎么做? 这是我的代码以便更好地理解

package com.example.tacoskingapp3;

import android.content.ClipData;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class ArticleManager extends SQLiteOpenHelper 

    public ArticleManager(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) 
        super(context, "Article.db", null, 1);
    

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) 
        String CreateTable = "CREATE TABLE articles (IdArticle INTEGER PRIMARY KEY  autoIncrement NOT NULL, NomArticle TEXT NOT NULL UNIQUE, TypeArticle TEXT NOT NULL, PrixArticle DECIMAL NOT NULL)";

        sqLiteDatabase.execSQL(CreateTable);
    

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVer, int newVer) 

    

【问题讨论】:

【参考方案1】:

...但直接在脚本中...

您想使用单独的脚本将数据插入数据库吗? (可能使用ADB命令来执行脚本之类的)

恐怕你做不到。 https://developer.android.com/training/data-storage/sqlite#DbHelper

关于您的描述,有一个简单的演示:click a button to insert data to DB,我重用了您的代码。看起来是这样的:

public class ArticleManager extends SQLiteOpenHelper 

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Article.db";
    public static final String TABLE_NAME = "articles";
    public static final String KEY_ID_ARTICLE = "IdArticle";
    public static final String KEY_NOM_ARTICLE = "NomArticle";
    public static final String KEY_TYPE_ARTICLE = "TypeArticle";
    public static final String KEY_PRIX_ARTICLE = "PrixArticle";

    private static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    KEY_ID_ARTICLE + " INTEGER PRIMARY KEY  autoIncrement NOT NULL, " +
                    KEY_NOM_ARTICLE + " TEXT NOT NULL UNIQUE ," +
                    KEY_TYPE_ARTICLE + " TEXT NOT NULL ," +
                    KEY_PRIX_ARTICLE + " DECIMAL NOT NULL)";


    public ArticleManager(@Nullable Context context) 
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) 
        Log.d("ArticleManager", "TANCOLO===> SQL_CREATE_TABLE = " + SQL_CREATE_TABLE);
        sqLiteDatabase.execSQL(SQL_CREATE_TABLE);
    

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVer, int newVer) 
    

public class ArticleEntity 
    String NomArticle;
    String TypeArticle;
    double PrixArticle;

    ArticleEntity(String NomArticle, String TypeArticle, double PrixArticle) 
        this.NomArticle = NomArticle;
        this.TypeArticle = TypeArticle;
        this.PrixArticle = PrixArticle;
    

public class TestSqliteScript extends AppCompatActivity 

    private ArticleManager articleManager;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_sqlite_script);

        articleManager = new ArticleManager(this);
        db = articleManager.getWritableDatabase();

        findViewById(R.id.create_data_into_db).setOnClickListener( view ->
                createDataIntoDB()
        );
    

    private void createDataIntoDB() 
        //step 1, crate ArticleEntity list
        List<ArticleEntity> entities = new ArrayList();
        entities.add(new ArticleEntity("Nom article 01", "type 1", 100.0));
        entities.add(new ArticleEntity("Nom article 02", "type 2", 200.0));
        entities.add(new ArticleEntity("Nom article 03", "type 3", 300.0));
        entities.add(new ArticleEntity("Nom article 04", "type 4", 400.0));

        //step 2, insert the data to DB
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) 
            entities.forEach(entity -> 
                ContentValues values = new ContentValues();
                values.put(KEY_NOM_ARTICLE, entity.NomArticle);
                values.put(KEY_TYPE_ARTICLE, entity.TypeArticle);
                values.put(KEY_PRIX_ARTICLE, entity.PrixArticle);

                long newRowId = db.insert(TABLE_NAME, null, values);
                Log.d("TestSqliteScript", "TANCOLO===> newRowId = " + newRowId );
            );
        
    

    @Override
    protected void onDestroy() 
        db.close();

        super.onDestroy();
    

它有效:

【讨论】:

以上是关于在 Android Studio 中插入数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Studio 上获取 Sqlite 数据库中插入数据的实时视图

如何仅在创建时将值插入数据库?(SQLite Android Studio)

在单击按钮时将数据插入 Android Studio 中的 SQlite 数据库

如何使用 Talend Open Studio 处理数百万条 MongoDB 记录并将其插入 Postgres

如何从表1中检索列数据并使用SQLite数据库将该列值插入到Android Studio的表2中?

如何使用 android studio 和 phpmyadmin(wampserver) 将数据插入数据库?