Android 以编程方式将数据手动插入sqlite表
如何以编程方式将数据输入/插入sqlite数据库。我搜索过类似的问题,但我看到的只是使用cmd或SDK工具 这是我的主要活动:-Android 以编程方式将数据手动插入sqlite表,android,android-sqlite,android-studio-3.0,Android,Android Sqlite,Android Studio 3.0,如何以编程方式将数据输入/插入sqlite数据库。我搜索过类似的问题,但我看到的只是使用cmd或SDK工具 这是我的主要活动:- public class MainActivity extends AppCompactActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.
public class MainActivity extends AppCompactActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
SQLiteDatabase db;
db = openOrCreateDatabase("books.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name,"
+ "genre,"
+ "description,"
+ "created_at TIMESTAMP,"")";
db.execSQL(CREATE_BOOKS_TABLE);
我想做一些类似的事情:
insert into books.db values (null, "Wheel of Time.epub", "fantasy", " Its absolutely the best", "2017-02-13 13:11:06");
有什么方法可以做到吗?您已经创建了表的列,但没有指定它们的类型。应该是
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name TEXT,"
+ "genre TEXT,"
+ "description TEXT,"
+ "created_at TIMESTAMP)";
对于“插入”,可以使用以下命令:
String sql =
"INSERT or replace INTO book_list (name, genre, description, created_at) VALUES('Wheel of Time.epub','fantasy','Its absolutely the best','2017-02-13 13:11:06')" ;
db.execSQL(sql);
更新
您可以在列中声明创建的以在插入时默认获取其值,如下所示:
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name TEXT,"
+ "genre TEXT,"
+ "description TEXT,"
+ "created_at TIMESTAMP not null default current_timestamp)";
这样,在执行insert时就不需要为其设置值:
String sql =
"INSERT or replace INTO book_list (name, genre, description) VALUES('Wheel of Time.epub','fantasy','Its absolutely the best')" ;
db.execSQL(sql);
您创建了表的列,但未指定其类型。应该是
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name TEXT,"
+ "genre TEXT,"
+ "description TEXT,"
+ "created_at TIMESTAMP)";
对于“插入”,可以使用以下命令:
String sql =
"INSERT or replace INTO book_list (name, genre, description, created_at) VALUES('Wheel of Time.epub','fantasy','Its absolutely the best','2017-02-13 13:11:06')" ;
db.execSQL(sql);
更新
您可以在
列中声明创建的以在插入时默认获取其值,如下所示:
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name TEXT,"
+ "genre TEXT,"
+ "description TEXT,"
+ "created_at TIMESTAMP not null default current_timestamp)";
这样,在执行insert时就不需要为其设置值:
String sql =
"INSERT or replace INTO book_list (name, genre, description) VALUES('Wheel of Time.epub','fantasy','Its absolutely the best')" ;
db.execSQL(sql);
这是一个非常普通的问题,但我可以想到的最简单的方法是不去上课:
将编辑文本和按钮拖动到活动中
将按钮附加到Edittext以将数据转换为字符串
使用上面的insert语句将数据放入数据库
编辑,此代码是否有助于您维护活动:
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SQLiteDatabase db = this.openOrCreateDatabase("books.db", MODE_PRIVATE, null);// Open or Create Database if none exists
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name TEXT,"
+ "genre TEXT,"
+ "description TEXT)";
db.execSQL(CREATE_BOOKS_TABLE); // Creates the fields for the database
Button btnData=findViewById(R.id.btnInsertData); // finds the button
etName=findViewById(R.id.etName); // Our text input box
// Set an OnClickListener for the button so when it's clicked, the code below is triggered
btnData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nameTxt=etName.getText().toString();
db.execSQL("INSERT INTO book_list (name, genre, description) VALUES ('"+nameTxt+"', 'fantasy', 'review')"); // Inserts the nameTxt data from the text box into the database, the other fields remain the same
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();// Makes a Toast to show the data is inserted
etName.setText("");// Clears the Textbox
}
});
}
}
您的Xml文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnInsertData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"/>
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name" />
</LinearLayout>
这将为数据库创建一个输入字段。这不是干净的代码,也不是最好的方法,但它很简单,并演示了如何获取文本输入并将其放入数据库。这是一个非常通用的问题,但我可以想到最简单的方法,而不必去上课:
将编辑文本和按钮拖动到活动中
将按钮附加到Edittext以将数据转换为字符串
使用上面的insert语句将数据放入数据库
编辑,此代码是否有助于您维护活动:
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SQLiteDatabase db = this.openOrCreateDatabase("books.db", MODE_PRIVATE, null);// Open or Create Database if none exists
final String CREATE_BOOKS_TABLE = "CREATE TABLE IF NOT EXISTS book_list ("
+ "id INTEGER primary key AUTOINCREMENT,"
+ "name TEXT,"
+ "genre TEXT,"
+ "description TEXT)";
db.execSQL(CREATE_BOOKS_TABLE); // Creates the fields for the database
Button btnData=findViewById(R.id.btnInsertData); // finds the button
etName=findViewById(R.id.etName); // Our text input box
// Set an OnClickListener for the button so when it's clicked, the code below is triggered
btnData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nameTxt=etName.getText().toString();
db.execSQL("INSERT INTO book_list (name, genre, description) VALUES ('"+nameTxt+"', 'fantasy', 'review')"); // Inserts the nameTxt data from the text box into the database, the other fields remain the same
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();// Makes a Toast to show the data is inserted
etName.setText("");// Clears the Textbox
}
});
}
}
您的Xml文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnInsertData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"/>
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name" />
</LinearLayout>
这将为数据库创建一个输入字段。这不是干净的代码,也不是最好的方法,但它很简单,并演示了如何获取文本输入并将其放入数据库。您是否查看了SQLiteDatabase
API文档?是的,但它只涉及将SQLite库直接导入应用程序,如果您需要使用最新版本的sqlite,则可以绕过Android内置的版本。它实际上没有太多关于语法的内容,所以没有插入新内容的方法?不,我在浏览文档时没有看到任何插入方法,所以没有SQLiteDatabase\insert()
方法?你真的检查了SQLiteDatabase
API文档了吗?你检查了SQLiteDatabase
API文档了吗?是的,但它只涉及将SQLite库直接导入到应用程序中,如果你需要使用最新版本的SQLite,就绕过安卓内置的版本。它实际上没有太多关于语法的内容,所以没有插入新内容的方法?不,我在浏览文档时没有看到任何插入方法,所以没有SQLiteDatabase\insert()
方法?你真的检查了SQLiteDatabase
API文档了吗?那么我如何插入?@Brendan Welcome:),在插入时使用时间戳可能会有问题,因为时间戳实际上是一个长的
值。如果您遇到问题,可以使用答案的更新部分。那么如何插入?@Brendan Welcome:),在执行插入时使用时间戳可能会有问题,因为时间戳实际上是一个长的
值。如果您遇到问题,可以使用答案的更新部分。