gpt4 book ai didi

java - 如何将数据从编辑文本插入到sqlite数据库?

转载 作者:行者123 更新时间:2023-11-29 21:11:07 26 4
gpt4 key购买 nike

当我运行该应用程序时,我得到:跳过 90 帧。应用程序可能在其主线程上进行了过多的工作。

主要代码:

public class Student_Data extends Activity implements OnClickListener { 
EditText studentName, StuClass;
Button b1;
String NameStudent;
String ClassStudent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student__data);

studentName= (EditText) findViewById(R.id.editText1);
StuClass= (EditText) findViewById(R.id.editText2);
NameStudent = studentName.getText().toString();
ClassStudent = StuClass.getText().toString();
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}

@Override
public void onClick(View v) {
//TODO Auto-generated method stub
insertData(NameStudent, ClassStudent);
}

private void insertData(String nameStudent2, String classStudent2) {
// TODO Auto-generated method stub
AndroidDbHelper helper = new AndroidDbHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(AndroidDbHelper.COLUMN_NAME, NameStudent);
content.put(AndroidDbHelper.COLUMN_CLASS, ClassStudent);

database.insert(AndroidDbHelper.TABLE_NAME, null, content);
}
}

AndroidDbHelper.java

public class AndroidDbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "StudentData";
public static final int DB_VERSION = 1;

public static final String TABLE_NAME = "student";
public static final String COLUMN_NAME= "name";
public static final String COLUMN_CLASS= "cclass";

public AndroidDbHelper(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
String CreateTable = "CREATE TABLE" + TABLE_NAME + "(" + COLUMN_NAME + "TEXT," + COLUMN_CLASS + "TEXT" + ");";
// TODO Auto-generated method stub
db.execSQL(CreateTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME );
onCreate(db);
}

虽然,我正在获取主要 Activity 页面以在模拟器上插入值..但如果我插入值,我会在 logcat 中收到错误。

04-02 11:31:08.095: E/AndroidRuntime(674): FATAL EXCEPTION: main
04-02 11:31:08.095: E/AndroidRuntime(674): android.database.sqlite.SQLiteException: near "TABLEstudent": syntax error (code 1): , while compiling: CREATE TABLEstudent(nameTEXT,cclassTEXT);
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.example.student_data.AndroidDbHelper.onCreate(AndroidDbHelper.java:25)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.example.student_data.Student_Data.insertData(Student_Data.java:41)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.example.student_data.Student_Data.onClick(Student_Data.java:36)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.view.View.performClick(View.java:4084)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.view.View$PerformClick.run(View.java:16966)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.os.Handler.handleCallback(Handler.java:615)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.os.Looper.loop(Looper.java:137)
04-02 11:31:08.095: E/AndroidRuntime(674): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-02 11:31:08.095: E/AndroidRuntime(674): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:31:08.095: E/AndroidRuntime(674): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-02 11:31:08.095: E/AndroidRuntime(674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-02 11:31:08.095: E/AndroidRuntime(674): at dalvik.system.NativeStart.main(Native Method)

最佳答案

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student__data);

studentName= (EditText) findViewById(R.id.editText1);
StuClass= (EditText) findViewById(R.id.editText2);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
NameStudent = studentName.getText().toString();
ClassStudent = StuClass.getText().toString();
insertData(NameStudent, ClassStudent);
}

您在 OnCreate 时获取的 String 将始终为您提供空 String 。将其写入 onClick 方法将给您完美的输出。

String CreateTable = "CREATE TABLE"+ TABLE_NAME + "("+ COLUMN_NAME + "TEXT,"+ COLUMN_CLASS + "TEXT"+ ");";

将这一行替换为这一行

String CreateTable = "CREATE TABLE "+ TABLE_NAME + "( "+ COLUMN_NAME + "TEXT, "+ COLUMN_CLASS + "TEXT"+ ");";

关于java - 如何将数据从编辑文本插入到sqlite数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22802883/

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