gpt4 book ai didi

java - 如何在 sqlite 数据库表中使用 JAVA 代码插入值?

转载 作者:行者123 更新时间:2023-11-30 03:04:59 24 4
gpt4 key购买 nike

这是我用来为我的应用程序在 sqlite 数据库中创建表的数据库帮助程序文件。

package net.learn2develop.listview;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.content.ContentValues;




public class DBHelper extends SQLiteOpenHelper
{

static String DB_NAME="AmericanDB.db";
static int DB_VERSION=1;

// TABLE 1
static String TABLE_NAME="Hex_Bolts";
static String ID=BaseColumns._ID;
static String Basic_Dia="Basic Diameter";
static String Actual_Dia="Actual Diameter";
static String Face_Width="Face Width";
static String Point_Width="Point Width";
static String Head_Height="Head Height";

public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DB_NAME,null,DB_VERSION);
// TODO Auto-generated constructor stub
}


@Override
public void onCreate(SQLiteDatabase db) {
String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, "
+Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ "text, "
+Point_Width+ "text, " +Head_Height+ "text)";
db.execSQL(query);



}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}


@Override
public SQLiteDatabase getWritableDatabase() {
// TODO Auto-generated method stub
return super.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("Basic Diameter", "0.2500");
values.put("Actual Diameter", "0.260");
values.put("Face Width", "0.438");
values.put("Point Width", "0.505");
values.put("Head Height", "0.188");
db.insert(TABLE_NAME, null, values);
db.close();
}

}

它包含一些错误,因为我不知道在数据库中插入数据值的方法,所以请引用这个并根据我的文件回答。谢谢。

最佳答案

改变

String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, " 
+Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ "text, "
+Point_Width+ "text, " +Head_Height+ "text)";

String query="create table " +TABLE_NAME+ "( " +ID+ " integer primary key AUTOINCREMENT, " 
+Basic_Dia+ " text, " +Actual_Dia+ " text, " +Face_Width+ " text, "
+Point_Width+ " text, " +Head_Height+ " text)";

需要在Face_WidthPoint_WidthHead_Height之后添加空格

关于java - 如何在 sqlite 数据库表中使用 JAVA 代码插入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com