繁体   English   中英

在android中查询sqlite(插入查询)

[英]Query for sqlite in android (insert query)

我想插入数据库..一个表单的值..其中两个是整数,一个是字符串..但我无法在我的sqlite数据库查看器中查看我输入的值..我正在使用以下码:

SQLiteDatabase myDB= null;
      String TableName = "basicdetai";
     try{ 

          myDB = this.openOrCreateDatabase("Medical", MODE_PRIVATE, null); 

          /* Create a Table in the Database. */
         myDB.execSQL("CREATE TABLE IF NOT EXISTS "
            + TableName
            + " (order_id INT(4), doc_id INT(4),doc_name VARCHAR );");
         /* Insert data to a Table*/
          myDB.execSQL("INSERT INTO "
            + TableName
            + "(order_id,doc_id,doc_name)"
           + " VALUES ("+ ordid + ","+ doci + ","+ docnam +");");
        // line 10  + " VALUES ("+ a + ","+ b + ","+ docnam +");");
        //  line 11  +"VALUES (5011,4011,'gupta');");



      }
      catch(Exception e) {
          Log.e("Error", "Error", e);
         } 
      finally {
           if (myDB != null)
            myDB.close();
          }

但是当我在上面的代码中使用第11 (5011,4011,'gupta')我可以通过sqlite浏览器看到记录(5011,4011,'gupta') 我正在执行以下操作,以将我从表单获取的字符串值转换为整数

try {
          ordid = Integer.parseInt(orderid.getText().toString());
          doci = Integer.parseInt(docid.getText().toString());
         // qn = Integer.parseInt(qty.getText().toString());
      } catch(NumberFormatException nfe) {
         System.out.println("Could not parse " + nfe);
      }

Plz帮助..

请尝试以下查询...

db.execSQL("insert into your table name (order_id,doc_id,doc_name)" + "values("ordid+","+doci+","
                + "\"" + docnam + "\"" + ") ;");

Android有一些帮助INSERT的帮助类。 你为什么不用它们?

例:

    public void addLibrary(LibraryItem item) {
    ContentValues row = new ContentValues();
    row.put("title", item.getTitle());
    row.put("author", item.getAuthor());
    row.put("publisher", item.getPublisher());
    row.put("thumbnailUrl", item.getThumbnail());
    row.put("formatType", item.getFormat());
    row.put("contentUrl", item.getContentUrl());
    row.put("publicationType", item.getPublicationType());
    row.put("favorite", item.isFavorite() ? "YES" : "NO");
    row.put("id", item.getItemId());
    row.put("normalizedTitle", StringUtil.normalize(item.getTitle()));
    row.put("downloaded", item.hasContent() ? Calendar.getInstance()
            .getTimeInMillis() : 0);
    row.put("wasdownloaded", item.hasContent() ? "YES" : "NO");
    row.put("bookmarked", "NO");
    row.put("archived", "NO");

    long id = db.insert("LIBRARY", null, row);
    }
Simple and easy to understand In this code i am using two fields.
//Activity.java

package com.virtual.university;

import java.util.Locale;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class VirtualActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
     private String tablename="StudentInfo";
     EditText t1,t2;
     Button b1,b2;
     SQLiteDatabase db;
     public static final String CONTENT1 = "sid";
     public static final String CONTENT2 = "pasw";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        t1=(EditText)findViewById(R.id.editText1);
        t2=(EditText)findViewById(R.id.editText2);
        b1=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2=(Button)findViewById(R.id.button1);
        b2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                t1.setText("");
                t2.setText("");
            }
        });
        // create or open database file
        db = openOrCreateDatabase("Login.db" , SQLiteDatabase.CREATE_IF_NECESSARY,null);
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);

        //Create table
        db.execSQL("CREATE TABLE IF NOT EXISTS "+tablename+" " +
                "( "+CONTENT1+" INTEGER PRIMARY KEY AUTOINCREMENT," +
                "  "+CONTENT2+" TEXT ); ");
       //TextView txtView=(TextView)findViewById(R.id.txtView);
       //txtView.setText("Table created");
    }
    public void onClick(View v) 
    {
    insert();   
    }
    private void insert() {

        int uid=Integer.parseInt(t1.getText().toString());
        String pwd=t2.getText().toString();
        String sql1 = "insert into " +tablename+ " (" +CONTENT1+ ", " +CONTENT2+ ") values(" +uid+  ",'" +pwd+ "')";
        db.execSQL(sql1);
        Toast.makeText(getBaseContext(),"inserted", Toast.LENGTH_SHORT).show(); 
    }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM